Python Django用户登录系统

Django用户登录体统

        • 创建app
        • 数据库模型设计
        • admin后台
        • URL和视图
        • 登录视图
        • 表单

创建app

python manage.py startapp login

数据库模型设计


from django.db import models
 
 
class User(models.Model):

    gender = (
        ('male','男'),
        ('female','女'),
    )
 
    name = models.CharField(max_length=128,unique=True)
    password = models.CharField(max_length=256)
    email = models.EmailField(unique=True)
    sex = models.CharField(max_length=32,choices=gender,default='男')
    c_time = models.DateTimeField(auto_now_add=True)
 
    def __str__(self):
        return self.name
 
    class Meta:
        ordering = ['c_time']
        verbose_name = '用户'
        verbose_name_plural = '用户'

建立完数据库之后执行数据迁移:

python manage.py makemigrations
python manage.py migrate

admin后台

在admin中注册模型:

from django.contrib import admin
from . import models
 
admin.site.register(models.User)

创建超级管理员:

python manage.py createsuperuser

之后就可以直接在admin上操作了,添加几个测试用户。

URL和视图


from django.conf.urls import url
from django.contrib import admin
from login import views
 
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^login/', views.login),
]

引入bootstrap:

https://v3.bootcss.com/getting-started/#download

根目录下新建一个static目录,并将解压后的bootstrap-3.3.7-dist目录,整体拷贝到static目录中。

由于Bootstrap依赖JQuery,所以我们需要提前下载并引入JQuery。

在static目录下,新建一个css和js目录,作为以后的样式文件和js文件的存放地,将我们的jquery文件拷贝到static/js目录下。

然后打开项目的settings文件,在最下面添加配置,用于指定静态文件的搜索目录:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

创建base.html模板:



{% load staticfiles %}
 


  
    
    
    
    
    {% block title %}base{% endblock %}
 
    
    
 
    
    
    
    {% block css %}{% endblock %}
  
  
    
 
    {% block content %}{% endblock %}
 
 
    
    
    
    
  

登录页面—login.html:


{% extends 'login/base.html' %}
{% load staticfiles %}
{% block title %}登录{% endblock %}
{% block css %}
    
{% endblock %}
 
 
{% block content %}
    
{% endblock %}

login.css:

body {
  background-color: #eee;
}
.form-login {
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.form-login .form-control {
  position: relative;
  height: auto;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
  padding: 10px;
  font-size: 16px;
}
.form-login .form-control:focus {
  z-index: 2;
}
.form-login input[type="text"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-login input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

登录视图


def login(request):
    if request.method == "POST":
        login_form = UserForm(request.POST)
        message = "请检查填写的内容!"
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            try:
                user = models.User.objects.get(name=username)
                if user.password == password:
                    return redirect('/index/')
                else:
                    message = "密码不正确!"
            except:
                message = "用户不存在!"
        return render(request, 'login/login.html', locals())
 
    login_form = UserForm()
    return render(request, 'login/login.html', locals())

表单

from django import forms
 
 
class UserForm(forms.Form):
    username = forms.CharField(label="用户名", max_length=128)
    password = forms.CharField(label="密码", max_length=256, widget=forms.PasswordInput)

原文链接:https://blog.csdn.net/laikaikai/article/details/80563387

你可能感兴趣的:(Python Django用户登录系统)