| 1044 | |
| 1045 | Extending the Profile system |
| 1046 | ============================ |
| 1047 | When 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... |
| 1048 | Django 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 | |
| 1050 | The 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 | |
| 1052 | Let's create a new application. From console: |
| 1053 | python manage.py startapp profile |
| 1054 | |
| 1055 | Now 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 | |
| 1057 | from django.db import models |
| 1058 | from django.contrib.auth.models import User |
| 1059 | |
| 1060 | class 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() |
| 1068 | Next, 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" |
| 1071 | in this case: |
| 1072 | AUTH_PROFILE_MODULE = "profile.profile" |
| 1073 | Once 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). |
| 1074 | We add now the profile application to the INSTALLED_APPS in settings.py: |
| 1075 | INSTALLED_APPS = ( |
| 1076 | 'django.contrib.auth', |
| 1077 | 'django.contrib.contenttypes', |
| 1078 | 'django.contrib.sessions', |
| 1079 | 'django.contrib.sites', |
| 1080 | 'django.contrib.admin', |
| 1081 | 'profile', |
| 1082 | ... |
| 1083 | ) |
| 1084 | It's time to create the table/s, from the console: |
| 1085 | python manage.py syncdb |
| 1086 | To provide access to the profile, we add the next line to urls.py: |
| 1087 | (r'^accounts/profile/$', 'profile.views.profile' ), |
| 1088 | And then, define the logic within /profile/views.py, this is a very simple example: |
| 1089 | from django.http import HttpResponse |
| 1090 | from django.template import RequestContext, loader |
| 1091 | from django.contrib.auth.decorators import login_required |
| 1092 | |
| 1093 | @login_required |
| 1094 | def profile(request): |
| 1095 | t = loader.get_template('accounts/profile.html') |
| 1096 | c = RequestContext(request,{}) |
| 1097 | return HttpResponse(t.render(c)) |
| 1098 | |
| 1099 | The 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. |
| 1100 | This is a simple template to show basic data about the user (/templates/accounts/profile.html): |
| 1101 | <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> |
| 1102 | <p>Click here if you want to Logout: <a href="/cuentas/logout">Logout</a> </p> |
| 1103 | As you can see, the profile it's easily yet powerful extensible. |
| 1104 | Sin fin-de-línea al final del archivo |