django--Login Instance

Source: Internet
Author: User

1. Preparatory work

Create the necessary directories and files, import js,css,bootstrap, etc., the directory structure is as follows:



2. Add static path to configuration file settings.py
1234 STATIC_URL = ‘/static/‘STATICFILES_DIRS = (    os.path.join(BASE_DIR,‘static‘),)


3. Reference file (js\css\bootstrap) login.html
12345678910111213 <!DOCTYPE html>"en">    <meta charset="UTF-8">    <title>登录</title>    <link rel="stylesheet" href="/static/plugin/bootstrap/css/bootstrap.css">    <link rel="stylesheet" href="/static/css/common.css"><body>    <script type="text/javascript" src="/static/js/jquery-2.1.4.min.js"></script>    <script type="text/javascript" src="/static/plugin/bootstrap/js/bootstrap.js"></script></body>


4. Find a login box in the Bootstrap Web pagelogin.html

12345678910111213141516171819 <form class="form-horizontal">  <div class="form-group">    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>    <div class="col-sm-10">      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">    </div>  </div>  <div class="form-group">    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>    <div class="col-sm-10">      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">    </div>  </div>  <div class="form-group">    <div class="col-sm-offset-2 col-sm-10">      <button type="submit" class="btn btn-default">Sign in</button>    </div>  </div></form>

5. Add Route urls.py
12345 from app01 importviewsurlpatterns = [    url(r‘^admin/‘, admin.site.urls),    url(r‘^login/‘, views.login),]
app01/views.py
12 def login(request):    returnrender(request,‘login.html‘)


6. Where form forms are submittedlogin.html
1 <form class="form-horizontal"action="/login/">


7. Input Set Name propertylogin.html

12 <input type= "email" class = "Form-control" Code class= "AS3 Plain" >name= "email" placeholder= "Email" > < Input type= "password" class = " Form-control " name= "pwd" placeholder= "Password" >

8. How to define form submission: metho?d= ' post 'login.? HTML

1 <form class="form-horizontal"action="/login/"method="post">

9, determine the user request method, modify the login function
    • If it is a GET request, return to the login.html page

    • If it is a POST request, verify that the user input is correct, jump on it, and then prompt the error

views.py
1234567891011 from django.shortcuts import renderfrom django.shortcuts import redirectdef login(request):    if request.method == ‘POST‘:        input_email = request.POST[‘email‘]        input_pwd = request.POST[‘pwd‘]        if input_email == ‘[email protected]‘ and input_pwd == ‘123‘:            return redirect("http://www.etiantian.org")        else:            return render(request,"login.html",{"status":"用户名密码错误"})    return render(request,‘login.html‘)


10. Add a span tag with the error prompt after the submit buttonlogin.html

12 <button type="submit"class="btn btn-default">Sign in</button>   <span style="color: red">{{ status }}</span>


11, temporarily block the form submitted 403 errors (later cross-site request forgery will be resolved) settings.py
12345678910 MIDDLEWARE_CLASSES = [    ‘django.middleware.security.SecurityMiddleware‘,    ‘django.contrib.sessions.middleware.SessionMiddleware‘,    ‘django.middleware.common.CommonMiddleware‘,    # ‘django.middleware.csrf.CsrfViewMiddleware‘,      #把此行注释    ‘django.contrib.auth.middleware.AuthenticationMiddleware‘,    ‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,    ‘django.contrib.messages.middleware.MessageMiddleware‘,    ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,]


12. Verification Results











From for notes (Wiz)

django--Login Instance

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.