Reputation: 31
I am learning django. Right now, I need to create a Userprofile.
I already created the model which is
class UserProfile(models.Model):
user = models.OneToOneField(User)
active = models.BooleanField()
address = models.CharField('Shipping address', max_length=150, blank=True, null=True)
telephone = models.CharField('Telephone number for shipping', max_length=20, blank=True, null=True)
steps = models.DecimalField('Steps since creation', max_digits=100, decimal_places=2, null=True)
active = models.BooleanField()
def __str__(self):
return "%s's profile" % self.user
inside an application called accounting. I already created
def create_user_profile(sender, **kwargs):
#When creating a new user, make a profile for him or her.
u = kwargs["instance"]
if not UserProfile.objects.filter(user=u):
UserProfile(user=u).save()
post_save.connect(create_user_profile, sender=User)
So every time a user is created, a profile created automatically. I've already created and verified that the user was created in userprofile table. I also went the shell. I looked for that user which ID is 4. I printed adress for User 4 and I got the address. So I am sure they are linked and working. But when I go to the HTML, i get the error.
Here is the View.
from accounting.models import UserProfile, Charge, Wallet
from django.shortcuts import get_object_or_404, RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import Context, loader
from django.contrib.auth.forms import UserCreationForm
#def userprofile(request, user_id):
def userprofile(request, user_id):
user_profile = request.user.get_profile()
active = user_profile.active
return render_to_response('accounting/templates/userprofile.html', {
'user_profile': user_profile,
'active': active,
}, context_instance=RequestContext(request))
Thanks.
Upvotes: 2
Views: 3208
Reputation: 53981
Make sure you have set AUTH_PROFILE_MODULE = 'my_profile_app.UserProfile'
in settings.py
Upvotes: 2
Reputation: 33410
Instead of:
request.user.get_profile()
Use:
request.user.userprofile
After years of Django development, never needed AUTH_PROFILE_MODULE or get_profile(). I don't know what's the advantage of using get_profile() (if any) but it seems like un-needed hassle.
Actually, I go through even less hassle by using django-annoying's AutoOneToOneField: https://bitbucket.org/offline/django-annoying/wiki/Home
More about OneToOne: https://docs.djangoproject.com/en/dev/topics/db/models/#one-to-one-relationships
Upvotes: 0