Introduction: Simple Landing interface display, as well as user login log out, the last user rights issues
Body:
You first need to set the root_urlconf in settings.py, the default value is:
root_urlconf = ' Www.urls ' #用户请求django站点页面首先检查这个模块
Here's www.urls is my project www under the urls.py file
The urls.py file mainly sets the Urlpatterns parameter, which is set as follows:
urlpatterns= [
URL (r ' ^$ ', ' Login.views.login_view ', name= ' Login_view '), #用户登陆界面
URL (r ' ^admin/', include (Admin.site.urls)), #后台登陆
]
Here to clean up the following ideas, when we visit the Web page first check the root_urlconf module, by observing the urls.py file urlpatterns parameter settings, the Web page first run the login app in the views.py Login_view function, When the user entered the account and password and click the login button, determine whether the account and password is correct, if wrong, return to the home page, correct access to the home page.
The views.py file is as follows:
#coding: Utf-8from django.shortcuts import render,httpresponsefrom django.contrib.auth import authenticate, logout, Loginfrom. Forms Import Loginformdef Login_view (Request): Logout (Request) if Request.method = = ' POST ': #判断是否为一个POST请求 form = LoginForm (Request. POST) #绑定数据至表单 if Form.is_valid (): #判断表单数据是否合法 uname = form.cleaned_data[' username ') #获取表单中用户名称 Pword = form.cleaned_data[' password '] #获取表单中用户密码 user = Authenticate (username=uname, Password=pword) #调用authentic Ate Authenticated Users if user is not none:if user.is_active:login (request, user) return render (Request, ' home/index.html ') # Redirect to a success page. Else:response = HttpResponse () Response.Write (' The above view file shows the user login process is broadly divided into: Authenticated Users--Login users--log out of the user three steps this three major steps, authentication users use function authenticate, login user login, log out user use Logout
The template format is as follows:
<Divclass= "Login"> <DivID= "Main"> <formMethod= "POST">{% Csrf_token%} {{form.as_p}}<inputtype= "Submit"value= "Submit" /> </form> </Div> </Div>
Finally, remember that you must log on to users before you can use user rights, the method of using permissions in the template (excerpt from the official):
authentication data in the template¶When you use requestcontext , the currently logged in user and their permissions are accessible in the template context .
Technical details
Technically, these variables only work when you use RequestContext and enable ' Django.contrib.auth.context_processors.auth '
Context processor can be accessed in the context of the template. It is the default generated configuration file. For more information, see the requestcontext documentation . User¶When rendering the requestcontext template, the currently logged-on user may be
Instance orAnonymoususerinstance, which is stored in the template variable{{ user }} In%} }}. Thanks for logging in.</p><p>welcome, new user. Please log in.</p>%}
If you are not using requestcontext, you cannot access the template variable:
Permissions¶The permissions of the currently logged on user are stored in the template variable {{ perms }}
In This is a django.contrib.auth.context_processors instance of the encapsulation, he is a template-friendly permissions agent. In {{ perms }}
object, the lookup of a single property is user.has_module_perms The agent. If the logged-in user has any license in the Foo app, this example displays True:Perms.}}
The lookup of a level two attribute is a proxy for user.has_perm.
This example displays True If the logged-on user has foo.can_vote 's license:Perms. Foo.}}
So, you can use the template {% if %}
statement Check permissions:{%IfPerms.foo%}<p>you has permission to does something in the Foo app.</p> {% if perms.foo.can_vote %} <p>you can vote! </p> {% endif %} {% if perms.foo.can_drive %} <p>you can Drive!</p> {% endif %}{% else %} <p>you don ' t has permission to does anything in the Foo app . </p>{% endif %}
You can also pass {% if in %}
statement Query permissions. For example:<p>in lookup works, too. %}%}
[Django] Login interface and User Login login permissions