Im working on an app where pet users can create profile and upload pictures of their pets.
I got this error when I access the profile page which I can't seem to understand. I created an app similar to this recently but ever seen I associated my models to User. I got errors tumbling down. I appreciate all the help from everyone and I thank you all.
I'm trying to let users upload a picture of themself at the profile page.
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()
I think this error is relating to my models and my Profile function at views.py
My models.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
Parts of my views.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)
)
My forms.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',)
@master!
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))