Django 注册、展示、登陆功能

Django 注册、展示、登陆功能

环境:

pycharm 201902
Python 2.7.13
Django 1.11.6

一、注册:

1. 新创建 project    File->NewProject->Django->选择项目存放路径,命名 test18 ->下拉 -> 选择 Existing interpreter (Python2.7)
2. cmd 新建应用
python manage.py startapp student
3.编辑 settings.py 文件,新增加app 名称:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'student',
]
4.配置test18/urls 
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^register/'include('student.urls'))
]
5.在student 目录下新增加 urls.py

#coding=utf-8

from django.conf.urls import url
import views

urlpatterns = [
    url(r'^$',views.index_views)
]

6.编辑 student/views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index_views(request):
    # 获取请求参数,即获取请求方式
    m = request.method
    #页面渲染,获得注册页面,为GET 请求
    if m == 'GET':
        return render(request,'register.html')
    #action="/register/" method="post",定死了提交需要用到post 方式,post 请求需要将获取到的数据注册到数据库里
    elif m == 'POST':
        uname = request.POST.get('uname','')
        pwd = request.POST.get('pwd','')
        return HttpResponse(m)
7.编辑 temlates 下的register.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/register/" method="post">
    {% csrf_token %}
    <p>
        <label>用户名: </label> <input type="text" name="uname"/>
    </p>
    <p>
        <label>免密: </label> <input type="password" name="pwd"/>
    </p>
    <p>
        <input type="submit" value="注册"/>
    </p>
</form>
</body>
</html>

8.启动服务后本地浏览器输入:http://127.0.0.1:8000/register/

出现注册页面
点击注册后出现 POST 

9.与数据库进行交互
编辑 student/models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Student(models.Model):
    sname = models.CharField(max_length=30,unique=True)
    spwd = models.CharField(max_length=30)


10.在 windows cmd 执行命令
python manage.py migrate # 创建表结构
python manage.py makemigrations student # 让 Django 知道我们在我们的模型有一些变更
python manage.py migrate student# 创建表结构

11.navicat 打开后出现 student_student 表,查看表结构,有 id sname spwd ,接下来可以操作数据库
12.编辑 student/views.py 增加post 之后的逻辑,逻辑为将注册的数据插入到数据库中
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from student.models import Student


def index_views(request):
    # 获取请求参数,即获取请求方式
    m = request.method
    #页面渲染,获得注册页面,为GET 请求
    if m == 'GET':
        return render(request,'register.html')
    #action="/register/" method="post",定死了提交需要用到post 方式,post 请求需要将获取到的数据注册到数据库里
    elif m == 'POST':
        #接收参数并为变量赋值
        uname = request.POST.get('uname','')
        pwd = request.POST.get('pwd','')
        #判断是否非空,不为空点击注册按钮会插入数据到数据库中,并打印注册成功!如果为空会打印注册失败
        if uname and pwd:
            stu = Student(sname=uname,spwd=pwd)
            stu.save()
            return HttpResponse('注册成功!')
        return HttpResponse('注册失败!')

13. 查看数据库,已将注册的数据插入到数据库中!http://127.0.0.1:8000/register/

二、显示注册信息

要求效果,访问http://127.0.0.1:8000/student/show/

1.编辑 test18/urls.py
新增加 url
url(r'^student/',include('student.urls'))

2.编辑 student/urls.py,新增加url 
url(r'show/$',views.show_views)
3. 编辑 student/views.py
def show_views(request):
    #查询 student 表中的所有数据
    stus = Student.objects.all()
    return render(request,'show.html',{'students':stus})
4.新增加 templates/show.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1" cellspacing="0" width="500px">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>密码</th>
        </tr>
       {% for stu in students %}
           <tr>
                <td>{{ stu.id}}</td>
                <td>{{ stu.sname}}</td>
                <td>{{ stu.spwd}}</td>
           </tr>
        {% endfor %}
    </table>
</body>
</html>

运行实现效果!

三、 登陆验证

1.修改 settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mypro',
        'HOST':'127.0.0.1',
        'PORT':'3306',
        'USER':'root',
        'PASSWORD':'123456'
    }
}
需要提前登陆数据库创建 mypro 库

2.改用mysql 数据库,需要安装mysql-python
下载一个MySQL-python-1.2.3.win-amd64-py2.7.exe文件进行安装
官网地址:https://pypi.python.org/pypi/MySQL-python/
3.打开windwos cmd 执行

pip install MYSQL-python==1.2.5
pip install pymysql

4. 打开 __init__.py 添加内容:
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()

5.windows 执行 python manage.py migrate (因为之前已有模型)
6.启动项目,没有报错
7.修改 student/urls.py,增加内容
    url(r'login/$',views.login_views)
8.新增加 student/views.py
def login_views(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        #1. 获取请求参数
        uname = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        #2.查询数据库
        if uname and pwd:
            c = Student.objects.filter(sname=uname,spwd=pwd).count()
            if c == 1:
                return HttpResponse('登陆成功')
        #判断是否登陆成功
        return HttpResponse('登陆失败')
访问:
http://127.0.0.1:8000/student/login/

posted @ 2020-12-01 10:34  lixinliang  阅读(232)  评论(0编辑  收藏  举报