Jump Django analog information from the user database to take home

Django simulation taken from the user information database, goto.

 

1, taking the user data database. We first need to build model model.

from django.db import models

class UserInfo(models.Model):

    username = models.CharField(max_length=32)

    password = models.CharField(max_length=64)

     def __str__(self):

        return self.username

 Analysis def __str __ (self): method.

 

  This process results plus

  

 

2, the configuration database login login account password.

Insert data first two records.

 

 Check the data inside the specified data

We need to create models.

obj = models.UserInfo.objects.filter(username=user, password=pwd).first()

 Front-end pass over the data.

user = request.POST.get ( 'user', None) # avoid submitting space, abnormal
user = user.strip () # user input is to end the spaces spaces
pwd = request.POST.get ( 'pwd')

 

  Browser access,

 

 Since then completed and processed, the data from Helena account password.

Code issues.

Login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
  <form action="/login/" method="post">
      {% csrf_token %}
      <p>
          <label>用户名</label>
          <input name="user" type="text">
      </p>
      <p>
          <label>密码</label>
          <input name="pwd" type="pwd">
          <input type="submit" value="提交" >
          <span style="color: red;">{{error_msg }}</span>
      </p>
  </form>
</body>
</html>

 Index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
   <span>Welcome to <b>{{ loginuser }}</b></span>
</body>
</html>

 Views.py configuration.

#coding:utf-8
from django.shortcuts import render, HttpResponse, redirect
from loginb import models
def login(request):
    error_msg = ''
    if request.method == "GET":
        return render(request, 'login.html')
    elif request.method == "POST":
        user = request.POST.get('user', None) #避免提交空,时异常
        user = user.strip() #用户输入末尾有空格是去空格
        pwd = request.POST.get('pwd')
        print(user, pwd)
        obj = models.UserInfo.objects.filter(username=user, password=pwd).first()
        # print (obj)
        if obj:
            # res = redirect('/index')
            # res.set_cookie('loginuser 'user)
            # return res
            return redirect('/index')
        else:
            error_msg = "账号或者密码不对"
            return render(request, 'login.html', {'error_msg': error_msg})
    else:
        return render(request, 'login.html')
def index(request):
    user_list = models.UserInfo.objects.all()
    # print(useraa.query)  # 打印SQL语句
    return render(request, 'index.html', {'user_list': user_list,})

 Remaining problem,

How to pass the user information registration request to the jump page. It should help session configuration ?

 Problems with cookies. Configure the user logon only in the case, before you can use index, or jump page.

Views.py only need to modify the configuration information,

  To achieve the purpose: Only after login can access the index page, otherwise consistent stopped at login login page.

Guess you like

Origin www.cnblogs.com/sunnyyangwang/p/11613953.html