Django Note User Register login and permissions

Source: Internet
Author: User
Tags auth uuid

#urls. py from

django.conf.urls import URL from
django.contrib import admin

urlpatterns = [
    url (r ' ^ Admin/', admin.site.urls,
    url (r ' ^login/$ ', ' Blog.views.login '),  #登录
    url (r ' ^register/$ ', ' Blog.views.register '), #注册
]
#models. py from django.db Import models from django.contrib.auth.models import User #用户字段扩展 class UserProfile (models. Model): User = models. Onetoonefield (User) #实体关系 one-to-one #user_id = models. Integerfield (blank=false,null=false) desc = models. TextField (blank=true,null=true) QQ = models. Integerfield (Blank=true,null=true) class news (models. Model): List_fields = [' id ', ' new_thread ', ' new_title ', ' new_url ', ' new_time ', ' new_from ', ' from_url ', ' New_boby '] new_th Read =models. TextField (blank=true,null=true) new_title =models. TextField (blank=true,null=true) New_url =models. TextField (blank=true,null=true) New_tim =models. TextField (blank=true,null=true) New_from =models. TextField (blank=true,null=true) From_url =models. TextField (blank=true,null=true) New_boby =models. TextField (blank=true,null=true) Dele =models. Booleanfield (default=0) def __str__ (self): return models. Model.__str__ (self) #自定义权限 in fact, the database that is written below is implicitly created just codename different class Meta:  
        Permissions = (' Can_view ', ' can ' = new '), (' Can_add ', ' can add new '), (' Can_edit ', ' can edit new '), (' Can_delete ', ' can delete new ')
 #admin. py from django.contrib Import admin to blog.models import News,userprofile from Django The. Contrib.auth.models import User #定义 The extended Display area class Profileinline (admin. Stackedinline): Model = userprofile verbose_name = ' profile ' class useradmin (admin. Modeladmin): Inlines = (Profileinline,) admin.site.register (News) Admin.site.unregister (User) #注销User admin.site. Register (user,useradmin) #采用自定义的 
#views. py django.shortcuts Import Httpresponse,render_to_response,httpresponseredirect from Django.views.generic Import Listview,deleteview from Django Import forms from Django.contrib.auth.models import User,

Permission from blog.models import news,userprofile import UUID # Create your views here. def index (request): Users = User.objects.all () for User in Users:print User.userprofile.desc return H Ttpresponse (users) #用户输入框 class LoginForm (forms. Form): email = forms. Charfield (label= ' e-mail: ', max_length=100) pwd = forms. Charfield (label= ' Password ', widget=forms. Passwordinput) #指定为输入密码 xxxdef Register (Request): #注册 if (' email ' or ' pwd ') not in request. GET:LF = LoginForm () return render_to_response (' register.html ', {' LF ': LF}) LF =loginform (request. Get) email = lf.data[' email '] pwd = lf.data[' pwd '] user = user () UserProfile = UserProfile () user.user Name = UUID.UUID1 () #随机生成用户名 user.email = Email User.sEt_password (PWD) user.save () userprofile.user_id = user.id #用户扩展 userprofile.desc = ' null! ' UserProfile. QQ = ' 000000 ' user.user_permissions = [Permission.objects.get (codename= ' Can_view '), Permission.objects.get (codename=


' Can_add '] #给予 browsing, adding Permissions Userprofile.save () User.save () return Httpresponseredirect ('/login/') #注册完返回登录界面 DEF login (Request): #登录 if (' email ' or ' pwd ') not in request. GET:LF = LoginForm () return render_to_response (' login.html ', {' LF ': lf}) LF = LoginForm (Request. Get) email = lf.data[' email '] pwd = lf.data[' pwd '] try:user = User.objects.get (email=email) excep T User.DoesNotExist:pass else:if User.check_password (pwd): #密码是否正确 if User.has_perm (' Blo G.can_view '): #判断是否有 ' can_view ' permission #do something if you have this permission to define the corresponding action return HttpResponse ("Login:
            "+ User.email + ' You Can-new! ') return HttpResponse ("Login:" + user.emAil + ' you can\ ' t-new.
 ') return Httpresponseredirect ('/login/')
#login. html

<! DOCTYPE html>
#register. html

<! DOCTYPE html>

to test
Registered

Login

Landing success

Log in background View user-owned Permissions users extension


View Database


Background Admin user, user group permission activity flag, which controls whether the user has been activated. If this flag for a user account is turned off and the user tries to log in with it, he or she will not be able to log on to the system even if the password is correct. A member flag that controls whether the user can log in to the Admin interface (that is, if the user is a member of your organization)
User systems can be used to control access to public pages (that is, unmanaged pages) (see chap. 14th), which is useful for distinguishing between public and administrative users.

Super user flag, which gives users permission to add, modify, and delete any item in the admin interface. If a user account has this label
Log, all permission settings (even if none) are ignored.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.