Ticket #3483: authentication.profile.diff

File authentication.profile.diff, 3.9 KB (added by adrian@…, 17 years ago)

Patch adding "Extendig the profile system" to the documentation

  • authentication.txt

     
    1 =============================
     1=============================
    22User authentication in Django
    33=============================
    44
     
    10411041                return User.objects.get(pk=user_id)
    10421042            except User.DoesNotExist:
    10431043                return None
     1044
     1045Extending the Profile system
     1046============================
     1047When it's necessary to build a website with users management, it's quite probable you need to collect some information about the users, for example, a telephone number, number of children, a picture...
     1048Django provides a lightweight way of defining a “profile” object that’s linked to a given user; this profile object can differ from project to project, and can even handle different profiles for different sites served from the same database.
     1049
     1050The first step in creating a profile is to define a model that holds the profile information. The model can be stored in any application, or just create a new one to manage the profile data.
     1051
     1052Let's create a new application. From console:
     1053        python manage.py startapp profile
     1054
     1055Now it's time to edit the models of the profile application. The only requirement Django places on this model is that it have a unique ForeignKey to the User model; this field must be named user Other that that, you can use any other fields you like. Here’s a strictly arbitrary profile model:
     1056
     1057from django.db import models
     1058from django.contrib.auth.models import User
     1059
     1060class MySiteProfile(models.Model):
     1061    # This is the only required field
     1062    user = models.ForeignKey(User, unique=True)
     1063
     1064    # The rest is completely up to you...
     1065    favorite_band = models.CharField(maxlength=100, blank=True)
     1066    favorite_cheese = models.CharField(maxlength=100, blank=True)
     1067    lucky_number = models.IntegerField()
     1068Next, you’ll need to tell Django where to look for this profile object. You do that by setting the AUTH_PROFILE_MODULE setting to the identifier for your model. So, if your model lives in an app called myapp, you’d put this in your settings file:
     1069
     1070        AUTH_PROFILE_MODULE = "myapp.mysiteprofile"
     1071Once that’s done, you can access a user’s profile by calling user.get_profile(). This function will raise a SiteProfileNotAvailable exception if AUTH_PROFILE_MODULE isn’t defined, and it also might raise a DoesNotExist exception if the user doesn’t have a profile already (you’ll usually catch that exception and create a new profile at that time).
     1072We add now the profile application to the INSTALLED_APPS in settings.py:
     1073INSTALLED_APPS = (
     1074        'django.contrib.auth',
     1075    'django.contrib.contenttypes',
     1076    'django.contrib.sessions',
     1077    'django.contrib.sites',
     1078    'django.contrib.admin',
     1079    'profile',
     1080        ...
     1081)
     1082It's time to create the table/s, from the console:
     1083        python manage.py syncdb
     1084To provide access to the profile, we add the next line to urls.py:
     1085        (r'^accounts/profile/$', 'profile.views.profile' ),
     1086And then, define the logic within /profile/views.py, this is a very simple example:
     1087        def profile(request):
     1088        t = loader.get_template('accounts/profile.html')
     1089        c = RequestContext(request,{})
     1090        return HttpResponse(t.render(c))
     1091The content of the template depends of the data you want to show, and also the logic of the profile view. It would be easy to manage the data using the newforms library.
     1092This is a simple template to show basic data about the user (/templates/accounts/profile.html):
     1093        <p>This is the profile for the user {{ user.username }}. Last login:    {{ user.last_login|date:"l d \d\e F \d\e Y \a \l\a\s H:i:s"}}</p>
     1094        <p>Click here if you want to Logout: <a href="/cuentas/logout">Logout</a>       </p>
     1095As you can see, the profile it's easily yet powerful extensible.
     1096 Sin fin-de-línea al final del archivo
Back to Top