Django实现用户注册登录

2023-05-16

学习Django中:写一个用户注册登录系统,开始搞事情 =====O(∩_∩)O~=====

=================

Ubuntu

python 2.7.12

Django 1.10.4

IDE:Pycharm

Bootstrap

=================

新建项目:(我是直接用pycharm直接生成的)

使用终端:

(创建项目) django-admin.py startproject mysite

(进入mysite新建app) django-admin.py startapp app01

记得在settings.py里面添加app

 设计模型:

/mysite/app01/models.py:

复制代码

 1 from __future__ import unicode_literals
 2 
 3 from django.db import models
 4 
 5 # Create your models here.
 6 
 7 class User(models.Model):
 8     username = models.CharField(max_length=50)
 9     password = models.CharField(max_length=50)
10     email = models.EmailField()

复制代码

创建User类,存放 username、password、email三个字段

同步数据库:

Python manage.py makemigrations

python manage.py migrate

Django会自动创建一系列表

没有自动创建superuser.......咱们手动创建:

python manage.py createsuperuser

设计逻辑视图(views):(使用表单)

/mysite/app01/views.py:

复制代码

 1 #coding=utf-8
 2 from django.shortcuts import render,render_to_response
 3 from django.http import HttpResponse
 4 from django import forms
 5 from models import User
 6 # Create your views here.
 7 class UserForm(forms.Form):
 8     username = forms.CharField(label='用户名',max_length=50)
 9     password = forms.CharField(label='密码',widget=forms.PasswordInput())
10     email = forms.EmailField(label='邮箱')
11 
12 def regist(request):
13     if request.method == 'POST':    
14         userform = UserForm(request.POST)
15         if userform.is_valid():
16             username = userform.cleaned_data['username']
17             password = userform.cleaned_data['password']
18             email = userform.cleaned_data['email']
19 
20             User.objects.create(username=username,password=password,email=email)
21             User.save()
22 
23             return HttpResponse('regist success!!!')
24     else:
25         userform = UserForm()
26     return render_to_response('regist.html',{'userform':userform})
27 
28 def login(request):
29     if request.method == 'POST':
30         userform = UserForm(request.POST)
31         if userform.is_valid():
32             username = userform.cleaned_data['username']
33             password = userform.cleaned_data['password']
34 
35             user = User.objects.filter(username__exact=username,password__exact=password)
36 
37             if user:
38                 return render_to_response('index.html',{'userform':userform})
39             else:
40                 return HttpResponse('用户名或密码错误,请重新登录')
41 
42     else:
43         userform = UserForm()
44     return render_to_response('login.html',{'userform':userform})

复制代码

 

注释:

label:标签

widget:装饰

widget=forms.PasswordInput():设置密码字段

设计模板文件

在templates里面新建index.html、regist.html、login.html

regist.html

复制代码

<!DOCTYPE html>
{% load static %}
<html lang="zh-CN"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Regist</title>

    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="{% static 'css/ie10-viewport-bug-workaround.css' %}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{% static 'css/signin.css' %}" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="{% static 'js/ie-emulation-modes-warning.js' %}"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
<style>
        html,body{text-align:center;margin:0px auto;}
</style>
<body>
<h1>注册页面</h1>
<form method = 'post' enctype="multipart/form-data">
{{userform.as_p}}
<input type="submit" value = "Regist" />
</form>
</body>
</html>

复制代码

 

 login.html

复制代码

<!DOCTYPE html>
{% load static %}
<html lang="zh-CN"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>

    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="{% static 'css/ie10-viewport-bug-workaround.css' %}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{% static 'css/signin.css' %}" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="{% static 'js/ie-emulation-modes-warning.js' %}"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
<style>
        html,body{text-align:center;margin:0px auto;}
</style>
<body>
<h1>登录页面</h1>
<form method = 'post' enctype="multipart/form-data">
{{userform.as_p}}
<input type="submit" value = "Login" />
</form>
</body>
</html>

复制代码

 

 index.html

复制代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<style>
        html,body{text-align:center;margin:0px auto;}
</style>
<body>
<h1>Hello Word!</h1>
</body>
</html>

复制代码

 

 设计urls

/mysite/urls.py

复制代码

from django.conf.urls import url,include
from django.contrib import admin
from app01 import urls
import app01

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'',include(app01.urls)),
]

复制代码

 

 /mysite/app01/urls.py

复制代码

from django.conf.urls import url,include
from django.contrib import admin
import views
admin.autodiscover()

urlpatterns = [
    url(r'^index/$',views.index),
    url(r'^login/$',views.login),
    url(r'^regist/$',views.regist),

]

复制代码

 

 使用admin后台管理注册的用户

在models.py里面设计一个UserAdmin类,用来记录注册用户的信息

/mysite/app01/models.py

复制代码

from __future__ import unicode_literals
from django.contrib import admin
from django.db import models

# Create your models here.

class User(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)
    email = models.EmailField()

class UserAdmin(admin.ModelAdmin):
    list_display = ('username','password','email')

admin.site.register(User,UserAdmin)

复制代码

 

 同步一下数据库(方法同上)

 效果图

主页:

注册页:

登录页面:

 后台:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django实现用户注册登录 的相关文章

  • The file Tomcat.exe was not found... Either the CATALINA_HOME environment variable is not defin

    window下安装tomcat后报错 The file Tomcat exe was not found Either the CATALINA HOME environment variable is not defined correc
  • 一文读懂Focalloss

    最近有人问我focalloss是什么原理 xff0c 看不懂 xff0c 大多数网文看了还更朦胧 xff0c 于是我抽空整理了一篇文章帮助大家理解 Focal loss解决了什么问题 xff1f 降低易分样本权重 增加难分样本的权重训练 对
  • Linux集成环境部署tomcat+redis

    Linux环境部署 tomcat 43 redis Tomcat 安装配置Redis安装配置 Tomcat 安装配置 下载tomcat安装包 xff0c 将安装包上传至自己的linux服务器中 xff0c 将安装包传到linxu环境中的 u
  • docker部署监控: prometheus + Grafana(超详细)

    前言 之前我们有用到top free iostat等等命令 xff0c 去监控服务器的性能 xff0c 但是这些命令 xff0c 我们只针对单台服务器进行监控 xff0c 通常我们线上都是一个集群的项目 xff0c 难道我们需要每一台服务器
  • mysql存储过程实现同时多表写入,构造创建商品数据

    前言 通常在做性能测试的过程中 xff0c 我们需要构造一下性能测试数据 xff0c 有些可以通过调用API xff0c 直接构造数据 xff0c 但是可能会存在一些场景 xff0c 需要我们直接在数据库中插入数据 xff0c 通常我们对于
  • MySQL修改root密码的多种方法,你掌握了吗

    前言 我们在工作中都会用到mysql数据库 xff0c 也是最熟悉用的最多的 xff0c 无论是在本地安装mysql还是在Linux安装mysql xff0c 都需要给数据库配置用户名和密码 xff0c 时间一长我们就会忘记配置数据库的密码
  • 性能测试:数据库架构和SQL优化

    前言 有时候我们出去面试的时候 xff0c 会被问到是否有做过架构方面的优化 如果没有准备突然被问到的话通常会有点懵 那么我们这里来整理一下系统架构优化相关的知识 其实一般架构优化主要就分为数据库架构 xff0c 第二个就是应用程序架构 数
  • springboot整合mybatis实现增删改查

    前言 在学习Springboot过程中 xff0c 整合mybatis框架实现表数据的增删改查 xff0c 话不多说 xff0c 开始贴代码 xff01 Spring Boot提供了一个名为spring boot starter paren
  • 国内个人免费在SCI、IEEE等数据库下载文献方法

    膜拜CSDN上的大神们 xff0c 在技术和方法上为我们奉献的力量 xff01 xff01 xff01 xff01 1 打开IEEE官网 xff1a https ieeexplore ieee org Xplore home jsp 用英文
  • springboot读取yml文件中的list列表、数组、map集合和对象

    前言 springboot配置文件yml类型简单的风格 xff0c 十分受大家的欢迎 xff0c 支持字符string类型 xff0c 支持列表list类型 xff0c 支持集合map类型 xff0c 支持数组array类型 xff0c 支
  • JMeter 线上压测如何预防服务器被打挂

    通常我们在做线上压测的时候 xff0c 会遇到一个问题 xff0c 就是担心在线上压测的时候服务器被我们压挂掉 xff0c 由于是线上服务器 xff0c 挂掉之后再重启 xff0c 会比较麻烦 xff0c 因此 JMeter 提供了一个方法
  • JMeter 进行函数助手MD5加密

    JMeter 函数助手 MD5 加密 JMeter函数助手中 xff0c 提供了MD5加密的方法 xff0c 如图所示 xff0c 我们将内容 123456 进行加密 xff0c 可以看到加密成功了 下面我们来看看项目接口的请求参数 这是一
  • mysql监控sql执行情况

    要想进阶针对mysql学习乃至掌握mysql调优的基本技能 xff0c 监控mysql的执行情况必不可少 就像我们的代码 xff0c 如果不能debug xff0c 想要进行调优排错 xff0c 难度将会大大增加 所以今天我们就来讲解如何监
  • Linux搭建JMeter环境(超详细)

    安装JDK 官网链接 xff1a https www oracle com java technologies javase javase jdk8 downloads html 因为我虚拟机配置是arm xff0c 安装 Linux环境下
  • 性能测试:数据库连接池问题分析

    前言 今天我们来压测一个支付的接口 xff0c 10个并发 xff0c 压测5分钟 下面我们可以看到tps大概在200多 xff0c 响应时间在40ms左右 下面我们来看一下服务器的性能 xff0c 应用服务器的cpu使用率大概在60 左右
  • 解决docker容器时间和宿主机时间不一致问题

    新建容器之后 xff0c 发现容器的时间和宿主机的容器不一致 xff0c 我们先看一下宿主机的时间 xff0c 现在是北京时间 进入容器之后 xff0c 查看容器时间和宿主机的时间不一致 xff0c 因为我现在容器的时间已经正常了 xff0
  • 性能测试:数据库性能问题实战分析

    接口压测分析 现在我们来压测一个获取用户信息接口 xff0c 这个接口会涉及到数据库的数据查询 我们的项目是部署正在应用服务器上面的 xff0c 因此我们需要同时监控应用服务器和数据库服务器 那么下面我们来看一下tomcat的这台服务器 x
  • Springboot封装HTTPClient中get,post,json,put请求方法

    pom xml依赖如下 span class token operator lt span span class token operator span span class token operator span https span c
  • Python实现列表数据按字典key值嵌套排序

    前言 当实现Python列表中的嵌套数据 xff0c 需要对数据进行排序 xff0c 是不是脑海中已经思考各种for循环或者while循环列表中的数据 xff0c 然后对列表中的数据进行排序 xff1f 小编今天在做可视化图表统计自动化的时
  • qemu-img 将iso转换成qcow2/raw

    参考 xff1a moonfly KVM虚拟机导出最小化体积的qcow2镜像文件 qemu不能直接将iso转化成qcow2或者raw xff0c 但是使用iso创建虚机后会产生qcow2 raw作为虚机的硬盘 xff0c 因此 xff0c

随机推荐

  • mac pro M1(ARM)安装vmware虚拟机及centos8详细教程

    前言 mac发布了m1芯片 xff0c 其强悍的性能收到很多开发者的追捧 xff0c 但是也因为其架构的更换 xff0c 导致很多软件或环境的安装成了问题 xff0c 这次我们接着来看如何在mac m1环境下安装centos8 Centos
  • prometheus+grafana对数据库mysql监控

    安装 mysql span class token function docker span run span class token parameter variable name span mysql test span class t
  • 性能测试:Redis性能监控(redis-stat工具)

    redis 监控 redis 监控一共有两种方式 xff0c 一种是通过info命令 xff0c 还有一种是使用redis stat工具 两者其实本质是一样的 xff0c 不过一个是命令行的模式下查看监控数据 xff0c 而另外一种是图形化
  • springboot中controller层接收参数,servers层调用mapper层,一条sql搞定排序

    前言 很多小伙伴们在公司不管是测试C端产品还是B端产品 xff0c 都会测到排序的业务需求 xff1b 那么我们就会好奇排序是如何实现的呢 xff1f 下面我们开始介绍代码的实现 数据库建表 我们需要创建一个书籍book表结构 xff0c
  • SpringBoot+HttpClient+JsonPath提取A接口返回值作为参数调用B接口

    前言 在做java接口自动化中 xff0c 我们常常需要依赖多个接口 xff0c A接口依赖B xff0c C xff0c D接口的响应作为请求参数 xff1b 或者URL中的参数是从其他接口中提取返回值作获取参数这是必不可少的 那么怎么实
  • SpringBoot开发——热更新JRebel安装、激活及使用

    在开发项目时 xff0c 本地调试项目经常会因为某处修改而需要重新启动项目 xff0c 虽然可以在Edit Configuretions里面将Runing Application Update Policies里的选项设置成 Update
  • windows10环境下的RabbitMQ安装步骤(图文)

    记录下本人在win10环境下安装RabbitMQ的步骤 xff0c 以作备忘 第一步 xff1a 下载并安装erlang 原因 xff1a RabbitMQ服务端代码是使用并发式语言Erlang编写的 xff0c 安装Rabbit MQ的前
  • 用Postman生成测试报告

    newman newman是一款基于nodejs开发的可以运行postman脚本的工具 xff0c 使用Newman xff0c 可以直接从命令运行和测试postman集合 安装nodejs 下载地址 xff1a https nodejs
  • 关于uni app提示【打包时未添加push模块】的问题解决

    问题 临时拿到一个前端项目 xff0c 被要求将此项目通过android studio完成离线打包 xff0c 毕竟Hbuilder的在线打包看运气 xff0c 运气不好的要等好几十分钟 xff0c 非常不方便 然后就按照官方文档一个个进行
  • 同步异步&同步互斥&几种锁

    文章目录 同步异步同步 xff1a 异步 xff1a 同步互斥临界资源 xff1a 临界区 xff1a 同步 xff1a 互斥 xff1a 几种锁自旋锁spinlock互斥锁mutex读写锁rwlockRCU锁可重入锁和不可重入锁条件变量c
  • Ubuntu 开发环境搭建之samba共享配置(免密登录)

    作者 xff1a Ligo 20200508 1 安装samba xff1a sudo apt get install samba 安装smbclient xff1a sudo apt get install smbclient 2 新建共
  • 从 GitHub 上手动安装python包教程

    基本流程 第一步 xff0c 下载 xff08 https pypi org 自己输入要下载的库名称 xff09 第二步 xff0c 解压 xff0c 打开到出现 setup py 的文件目录 第三步 xff1b 打开 Windows Po
  • 记:解决ADB Interface 找不到驱动程序方法

    前言 由于之前一直在自己笔记本上跑数据 xff0c 公司台式机上一直没安装sdk xff0c 终于有时间了 xff0c 以为很快安装好 xff0c 没想到台式机碰到一堆坑 最后终于解决 xff0c 特此记录 xff0c 希望给有同样问题的小
  • os.system执行.py文件

    import os os system r 39 E start py 39 错误写法 xff0c 没声明文件类型 xff0c 默认按你系统文件原始格式打开 os system r 39 python E start py 39 正确写法
  • python图像识别 - paddleocr (小白安利款)

    前言 之前我有记载过 xff0c 关于Python的图像识别的文章 xff0c 但是识别率不高且不够灵活 xff0c 实用性不强 xff0c 所以不怎么推荐 最近发现一个新的Python写好的的轮子 paddleocr xff0c 本人也安
  • 【QtScrcpy】开源的投屏控制软件 - 安利

    前言 因为本人有在做群控项目 xff0c 有很多手机设备的开发和研究 xff0c 这里给大家介绍安利一个好用的安卓 电脑的投屏控制软件 xff0c 简洁好用 xff0c 不仅可以投屏 xff0c 还能反向批量控制 xff0c 并且它是免费的
  • 【解决】:error:Microsoft Visual C++ 14.0 is required.报错

    前言 有时候在新电脑上要安装个Python包 xff0c 会出现这个问题 xff0c 所以专门写篇文章做个记录 xff0c 方便以后好找 问题表现 xff1a 从上图中可以看到错误 error Microsoft Visual C 43 4
  • 逆向分析:还原 App protobuf 协议加密

    前言 之前有记录js逆向 安卓逆向等 xff0c 今天这里记录下一些协议逆向 xff0c 这种一般出现在websocket 协议 protobuf 协议等 xff0c 某音 xff0c B站 APP等都有用到这些协议加密 xff0c 而我们
  • 【解决】常见反爬总结之SVG映射

    前言 记得好早之前 xff0c 我做过关于外卖平台字体加密反爬的总结笔记 xff0c 今天给大家记录另外一种常见的反爬 SVG 映射 什么是SVG呢 xff1f SVG 全称为 Scalable Vector Graphics xff0c
  • Django实现用户注册登录

    学习Django中 xff1a 写一个用户注册登录系统 xff0c 开始搞事情 61 61 61 61 61 O O 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 6