2016-09-15 69 views
0

我在Tango的第9章用Django - 創建用戶認證。在註冊頁面中,我可以選擇上傳圖片。在我的管理文件中,我註冊自己後,一切看起來都不錯。我出現在用戶配置文件中,它甚至顯示我上傳的圖像: Picture: Currently: profile_images/earth.jpeg Clear。然而,當我點擊該圖片,這是錯誤消息我得到:Django - 探戈與Django上傳圖片

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:

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} 
       ) 

最後,我的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'因爲圖片的路徑是錯誤的? Url顯示在第一個代碼塊中。請求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'的用戶配置文件對象不存在。

看起來您的某個網址格式可能已關閉;它可能只是想捕獲該1用作查找的PK,而不是捕獲1/change/profile_images/earth.jpeg