我正在开发一个应用程序,宠物用户可以在其中创建个人资料并上传他们的宠物照片。
当我访问我似乎无法理解的个人资料页面时出现此错误。我最近创建了一个与此类似的应用程序,但曾经见过我将我的模型与用户相关联。我得到了错误。我感谢大家的所有帮助,我感谢大家。
我试图让用户在个人资料页面上传自己的照片。
pet_person.user_id may not be NULL
Traceback Switch to copy-and-paste view
C:\o\mysite\pet\views.py in Profile
form.save()
我认为这个错误与我的模型和我在 views.py 中的 Profile 函数有关
我的模型.py
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
class Person(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=100)
image = models.FileField(upload_to="images/",blank=True,null=True)
def __unicode__(self):
return self.name
class Pet(models.Model):
Person = models.ForeignKey(Person)
description = models.CharField(max_length=100)
image = models.FileField(upload_to="images/",blank=True,null=True)
def __unicode__(self):
return self.description
我的部分观点.py
def Profile(request):
form = PictureForm(request.POST ,request.FILES or None)
if form.is_valid():
form.save()
return render_to_response('profile.html',{'form':PictureForm()},context_instance = RequestContext(request)
)
我的表格.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from pet.models import *
class UserRegistration(ModelForm):
username = forms.CharField(widget = forms.Textarea)
name = forms.CharField(widget = forms.Textarea)
email = forms.EmailField(widget = forms.Textarea)
password = forms.CharField(widget = forms.PasswordInput(render_value=False))
password1 = forms.CharField(label =(u'Verify Password'), widget = forms.PasswordInput(render_value=False))
class Meta:
model = Person
exclude = ('user',)
fields = ('username','email','name','password','password1')
def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError("That user is already taken , please select another ")
def clean(self):
if self.cleaned_data['password'] != self.cleaned_data['password1']:
raise forms.ValidationError("The password does not match ")
return self.cleaned_data
class LoginForm(forms.Form):
username = forms.CharField(widget = forms.Textarea)
password = forms.CharField(widget = forms.PasswordInput(render_value=False))
model = Person
fields = ('username','password')
class PictureForm(forms.ModelForm):
class Meta:
model = Person
fields = ('image',)
@掌握!
def Profile(request):
form = PictureForm(request.POST ,request.FILES or None)
if form.is_valid():
event = form.save(commit=False)
event.user=request.user
event.save()
return render_to_response('profile.html',{'form':PictureForm()},context_instance = RequestContext(request))