I'm a beginner in Django. I need to setup a website, where each user has a profile page. I've seen django admin. The profile page for users, should store some information which can be edited by the user only. Can anyone point me out how that is possible?. Any tutorial links would be really helpful. Also, are there any modules for django, which can be used for setting up user page.
-
1What have you developed already? – Dean Jan 28 '12 at 15:50
-
4http://stackoverflow.com/questions/8177289/django-profiles http://stackoverflow.com/questions/7313336/django-multiple-user-profiles-subprofiles http://stackoverflow.com/questions/3100521/django-registration-and-multiple-profiles http://stackoverflow.com/questions/4171083/is-there-a-django-app-that-can-manage-your-users-profiles http://stackoverflow.com/questions/7173279/django-registration-and-django-profiles http://stackoverflow.com/questions/2654689/django-how-to-write-users-and-profiles-handling-in-best-way http://stackoverflow.com/questions/1910359/creating-a-extended-use – Timmy O'Mahony Jan 28 '12 at 16:20
2 Answers
You would just need to create a view that's available to an authenticated user and return a profile editing form if they're creating a GET
request or update the user's profile data if they're creating a POST
request.
Most of the work is already done for you because there are generic views for editing models, such as the UpdateView. What you need to expand that with is checking for authenticated users and providing it with the object that you want to provide editing for. That's the view component in the MTV triad that provides the behavior for editing a user's profile--the Profile
model will define the user profile and the template will provide the presentation discretely.
So here's some behavior to throw at you as a simple solution:
from django.contrib.auth.decorators import login_required
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import UpdateView
from django.utils.decorators import method_decorator
from myapp.models import Profile
class ProfileObjectMixin(SingleObjectMixin):
"""
Provides views with the current user's profile.
"""
model = Profile
def get_object(self):
"""Return's the current users profile."""
try:
return self.request.user.get_profile()
except Profile.DoesNotExist:
raise NotImplemented(
"What if the user doesn't have an associated profile?")
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
"""Ensures that only authenticated users can access the view."""
klass = ProfileObjectMixin
return super(klass, self).dispatch(request, *args, **kwargs)
class ProfileUpdateView(ProfileObjectMixin, UpdateView):
"""
A view that displays a form for editing a user's profile.
Uses a form dynamically created for the `Profile` model and
the default model's update template.
"""
pass # That's All Folks!

- 32,650
- 13
- 84
- 114
-
2
-
Thanks for this - very useful! Two things I had to change to make it work: Instead of `@login_required` you need `@method_decorator(login_required)` (imported from `django.utils.decorators`). And instead of using `BaseUpdateView` use `UpdateView`, or else there's an error about `render_to_response` being missing. – Phil Gyford Sep 19 '12 at 11:14
-
You can
- create another Model for storing profile information about user
- add
AUTH_PROFILE_MODULE='yourprofileapp.ProfileModel'
to settings.py In profile editing view, allow only logged in users to edit their own profiles
example:
@login_required def edit_profile(request): ''' edit profile of logged in user i.e request.user '''
You can also make sure that whenever new user is created the user's profile is also created using django's signals
Read about storing additional information about users from django documentation

- 1,176
- 8
- 7