2016-09-15 5 views
0

나는 장고에 장고 9 장고 장고 - 사용자 인증 생성. 등록 페이지에는 사진을 업로드 할 수있는 옵션이 있습니다. 내 관리 파일에서 내가 등록한 후에 모든 것이 잘 보입니다. 사용자 프로필에 표시되며 업로드 한 이미지도 표시됩니다 : Picture: Currently: profile_images/earth.jpeg Clear. 단지 레지스터() -장고 - 탱고 장고 함께 업로드

from __future__ import unicode_literals 

from django.db import models 
from django.template.defaultfilters import slugify 
from django.contrib.auth.models import User 


class Category(models.Model): 
    name = models.CharField(max_length=128, unique=True) 
    views = models.IntegerField(default=0) 
    likes = models.IntegerField(default=0) 
    slug = models.SlugField(unique=True) 

    def save(self, *args, **kwargs): 
     self.slug = slugify(self.name) 
     super(Category, self).save(*args, **kwargs) 

    class Meta: 
     verbose_name_plural = 'categories' 

    def __str__(self): 
     return self.name 


class Page(models.Model): 
    category = models.ForeignKey(Category) 
    title = models.CharField(max_length=128) 
    url = models.URLField() 
    views = models.IntegerField(default=0) 

    def __str__(self): 
     return self.title 


class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    website = models.URLField(blank=True) 
    picture = models.ImageField(upload_to='profile_images', blank=True) 

    def __str__(self): 
     return self.user.username 

views.py :

def register(request): 
    registered = False 

    if request.method == 'POST': 
     user_form = UserForm(data=request.POST) 
     profile_form = UserProfileForm(data=request.POST) 

     if user_form.is_valid() and profile_form.is_valid(): 
      user = user_form.save() 
      user.set_password(user.password) 
      user.save() 

      profile = profile_form.save(commit=False) 
      profile.user = user 

      if 'picture' in request.FILES: 
       profile.picture = request.FILES['picture'] 
      profile.save() 
      registered = True 
     else: 
      print user_form.errors, profile_form.errors 
    else: 
     user_form = UserForm() 
     profile_form = UserProfileForm() 

    return render(request, 'rango/register.html', 
        {'user_form': user_form, 
        'profile_form': profile_form, 
        'registered': registered} 
       ) 

Page not found (404) 
Request Method: GET 
Request URL: http://localhost:8000/admin/rango/userprofile/1/change/profile_images/earth.jpeg/change/ 
Raised by: django.contrib.admin.options.change_view 
user profile object with primary key u'1/change/profile_images/earth.jpeg' does not exist. 
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. 

models.py : 나는 그 사진을 클릭하면 그러나 이것은 내가 얻을 오류 메시지는 마지막으로, 내 register.html 파일 :

{% extends 'rango/base.html' %} 
{% load staticfiles %} 

{% block title_block %} 
    Register 
{% endblock %} 

{% block body_block %} 
    <h1>Register with Rango</h1> 
    {% if registered %} 
     Rango says: <strong>thank you for registering!</strong> 
     <a href="/rango/">Return to the homepage</a><br/> 
    {% else %} 
     Rango says: <strong>register here!</strong> 
     Click <a href="/rango/">here</a> to go to the homepage<br/> 
     <form id="user_form" method="post" action="/rango/register/" enctype="multipart/form-data"> 
      {% csrf_token %} 
      {{ user_form.as_p }} 
      {{ profile_form.as_p }} 

      <input type="submit" name="submit" value="Register" /> 
     </form> 
    {% endif %} 
{% endblock %} 
+0

그냥 추측하지만 그림의 경로가 잘못 되었기 때문에 404'ing을 생각하니? URL은 첫 번째 코드 블록'Request URL : http : // localhost : 8000/admin/rango/userprofile/1/change/profile_images/earth.jpeg/change /'에 나타납니다. – Noelkd

+0

그건 맞는 것 같습니다. URL 경로가 무엇인지는 모르겠다. 흥미롭게도, 내 디렉토리에 그림이있는 폴더를 만들었습니다. 내가 링크를 클릭했을 때 그들이 왜 표시되지 않는지 아시겠습니까? – Robby

+0

사용자 프로필 페이지를 생성하는 템플릿을 볼 필요가 있다고 생각하십니까? – Noelkd

답변

1

기본 키가 u'1/change/profile_images/earth.jpeg 인 사용자 프로필 개체가 없습니다.

URL 패턴 중 하나가 꺼져있는 것 같습니다. 조회를 위해 PK로 사용할 1을 캡처하려고하지만 대신 1/change/profile_images/earth.jpeg을 캡처하는 것입니다.