Ticket #3483: authentication.profile-v2.diff

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

added "login required" to the view, and other small changes

1Index: authentication.txt
3--- authentication.txt  (revisión: 5891)
4+++ authentication.txt  (copia de trabajo)
5@@ -1041,3 +1041,63 @@
6                 return User.objects.get(pk=user_id)
7             except User.DoesNotExist:
8                 return None
10+Extending the Profile system
12+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...
13+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.
15+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.
17+Let's create a new application. From console:
18+       python startapp profile
20+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:
22+from django.db import models
23+from django.contrib.auth.models import User
25+class MySiteProfile(models.Model):
26+    # This is the only required field
27+    user = models.ForeignKey(User, unique=True)
29+    # The rest is completely up to you...
30+    favorite_band = models.CharField(maxlength=100, blank=True)
31+    favorite_cheese = models.CharField(maxlength=100, blank=True)
32+    lucky_number = models.IntegerField()
33+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:
35+       AUTH_PROFILE_MODULE = "myapp.mysiteprofile"
36+in this case:
37+       AUTH_PROFILE_MODULE = "profile.profile"
38+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).
39+We add now the profile application to the INSTALLED_APPS in
41+       'django.contrib.auth',
42+       'django.contrib.contenttypes',
43+       'django.contrib.sessions',
44+       'django.contrib.sites',
45+       'django.contrib.admin',
46+       'profile',
47+       ...
49+It's time to create the table/s, from the console:
50+       python syncdb
51+To provide access to the profile, we add the next line to
52+       (r'^accounts/profile/$', 'profile.views.profile' ),
53+And then, define the logic within /profile/, this is a very simple example:
54+       from django.http import HttpResponse
55+       from django.template import RequestContext, loader
56+       from django.contrib.auth.decorators import login_required
58+       @login_required
59+       def profile(request):
60+               t = loader.get_template('accounts/profile.html')
61+               c = RequestContext(request,{})
62+               return HttpResponse(t.render(c))
64+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.
65+This is a simple template to show basic data about the user (/templates/accounts/profile.html):
66+       <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>
67+       <p>Click here if you want to Logout: <a href="/cuentas/logout">Logout</a>       </p>
68+As you can see, the profile it's easily yet powerful extensible.
69\ Sin fin-de-línea al final del archivo