我在Tango的第9章使用Django - 创建用户身份验证。在注册页面中,我可以选择上传图片。在我的管理文件中,注册后我的一切看起来都不错。我显示在用户配置文件中,它甚至显示我上传的图像:
cut -d: -f4 /etc/group | tr ',' '\n' | grep -v '^$' | sort | uniq -c
。但是,当我点击该图片时,这是我收到的错误消息:
Picture: Currently: profile_images/earth.jpeg Clear
models.py:
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.
views.py - 只有register():
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
最后,我的register.html文件:
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}
)
答案 0 :(得分:1)
主键u'1 / change / profile_images / earth.jpeg'的用户个人资料对象不存在。
看起来您的某个网址格式可能已关闭;它可能只是想捕获1
以用作查找的PK,而是捕获1/change/profile_images/earth.jpeg
。