user part is the basic function of a website, Django is a very good package of this part, we only need to do some simple modifications on the basis of Django can achieve the result that we want

first, I assume you to Django session, cookie and admin database, some have a certain understanding do not understand, can refer to this tutorial: http://djangobook.py3k.cn/2.0/

1,

first user login: imagine login interface:

log view code as follows:

 def userLogin (request): curtime=time.strftime ("%Y-%m-%d%H:%M:%S", time.l Ocaltime (if)); request.method=='POST'print ("POST") username=request.POST.get ('name' ') password=request.POST.get ('password' ') user= auth.authenticate (username=username, password=password) a*********** if user and user.is_active: auth.login (request, user) b************ return HttpResponseRedirect ("/user") return render_to_response ("blog/userlogin.html RequestContext" (request {'curtime', 

: curtime})

) Note: A, here is to use auth framework to verify the username and password Django, some people will say that this is not flexible, I want to use the mailbox login? Later we will say that the django.contrib.auth.models.User model is used directly to manipulate user data so that we can do what you want to verify.
B, the user information is verified after the user login information need to write simple

2,

session in the cancellation of cancellation of users just need to delete, the corresponding user information in the session OK

 def userLogout (request): auth.logout (request) return HttpResponseRedirect ('/user') 

,

3 users registered

registered

username and password, email is the basic registration information, which is contained in the Django, the telephone is the extension of the user information, so as to expand the user information, one can speak, I first revealed by profile expansion The way of exhibition (personal preference, I think this way is simple)

registration code:

 def view view userRegister (request): curtime=time.strftime ("%Y-%m-%d%H:%M:%S" (time.localtime)); if (request.user.is_authenticated): #a******************* return HttpResponseRedirect ("/user") try: if request.method=='POST': username=request.POST.get ('name', '' password1=request.POST.get ('password1'), password2=request.POST.get ('password2' ''), '') email=request.POST.get ('email'') phone=request.POST.get ('phone', errors=[]' registerForm=RegisterForm ({'username'): username,'password1': password1,'password2': password2,'email': email}) #b******** if not regist (erForm.is_valid): errors.extend (registerForm.errors.values) (return) render_to_response ("blog/userregister.html", RequestContext (request, {'curtime': curtime,'username': username,'email': email,'errors': errors}) if =password2: (errors.append) password1! "Two input password does not match!" (render_to_response return), RequestContext ("blog/userregister.html" request, {'curtime': curtime,'username': username,'email': email,'errors': errors})) filterResult=User.objects.filter (username=username) #c************ if len (filterResult) > 0: errors.append ("username already exists" render_to_response "blog/userregister.html" (return), RequestContext (request, {'curtime': curtime,'username': US Ername,'email': email,'errors': errors}) (user=User)) #d************************ user.username=username user.set_password (password1) user.email=email (user.save) profile profile=UserProfile (# user extension information) #e****************** profile.user_id=user.id profile.phone=phone (profile.save) * * * * * * * to verify newUser=auth.authenticate # before login (username=username, password=password1) #f*************** if newUser is not None: auth.login (request, newUser) #g************ * * * * * * * return HttpResponseRedirect ("/user") except Exception, e: errors.append (STR (E) render_to_response (return) blog/userregister.html, RequestContex T (request, {'curtime': curtime,'username': username,'email': email,'errors': errors}) return render_to_response ("blog/userregister.html"), RequestContext (request, {'curtime': curtime}) 

)

note:

a, the user login validation, it is not necessary to have Login registered (of course this is just practice the actual production situation, may not be the same as

b), a single registry over the data need some basic authentication, how to verify the form data can refer to this tutorial: http://djangobook.py3k.cn/2.0/chapter07/

c, User model is used to find the registered user name exists, if the user already exists requires prompt registration customers to replace the user name

d, using User model to verify by using The user data stored in the database, note that the set_password method is used to save the password information (because there is a process of encryption,

e

) extended information stored in the user's (here is the user's phone number), where the use of custom extended model UserProfile, specifically how to extend the user will need to verify the following

f, before the user logs in, or else will be wrong

g,

4, user login user

extended

online on the Django user extension there are several ways, people tend to Profile the way, this way is simple and clear, expansion steps are as follows: a new model of UserProfile

 from django.contrib.auth.models import User class UserProfile 

A, App models (in your mode Ls.Model (User): user=models.OneToOneField, unique=True, verbose_name= ('user')) #a****** phone=models.CharField (max_length=20) #b******

note: A, UserProfile is actually an ordinary model, and then through the User model this sentence and the establishment of Django

contact B,

B,

extended user information Python manage.py syncdb created userprofile in the database table in the

C 201821911126 "alt=", how to call the user extension information? Is very simple, to get user, then provided by user (get_profile) to get the profile object, such as

(

 user.get_profile.Phone

D), how to update and store user profile In fact, information, user registration section before we have used this function, userprofile is actually a model, as long as we get the user ID through the user model, it can be operated through corresponding UserProfile model profile

 (user=User): user.username=username user.set_password (password1) user.email=email (user.save) # user extension profile=UserProfile (profile.user_id=user.id profile.phone=phone) profile profile.save (

)

E, we can extend user information from the program, then I want to extend to edit the information in the background of the admin how to do it:

is very simple, just add the following in your APP statements in admin.py for

 class (UserProfileInline admin.StackedInline: model=UserProfile) Fk_name='user'max_num=1 class UserProfileAdmin (UserAdmin): inlines = [UserProfileInline, admin.site.unregister (User) admin.site.register (User, UserProfileAdmin) 

this is my experience when learning Django, perhaps not all right, only for reference, I hope to help everyone to learn, I hope you will support a script.

you might be interested in this article: "


This concludes the body part

This paper fixed link:http://www.script-home.com/examples-of-django-user-registration-login-logout-and-user-extension.html | Script Home | +Copy Link

Article reprint please specify:Examples of Django user registration, login, logout, and user extension | Script Home

You may also be interested in these articles!