一、环境版本信息:

操作系统:windows10

Django版本:2.0.5

Python版本:3.6.4

Mysql版本: 5.5.53   安装mysql

二、基础信息

1、App中的模型models.py

from django.db import models

# Create your models here.
class users(models.Model):
blog_username = models.CharField(max_length=20)
blog_link = models.CharField(max_length=50)
class account(models.Model):
blog_account = models.CharField(max_length=20)
blog_password = models.CharField(max_length=20)
blog_username = models.CharField(max_length=20)

app_users表插入数据:

app_account表插入数据:

2、在模板文件夹中新建londing.html

<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
</head>
<body >
<form action="/londing" method="get">
账号:<input type="text" name="account">
密码:<input type="text" name="password">
<input type="submit" value="登陆">
</form>
</body>
</html>

3、视图views.py

from django.shortcuts import render
from App.models import users,account
from django.http import HttpResponse
# Create your views here. def index(request):
user = users.objects.get(id=1)
context = {'user':user}
return render(request, 'index.html', context)
def londing_form(request): #添加表单页面
context = {}
return render(request,'landing.html',context)
def londing(request): #数据接收和处理
if 'account' in request.GET:
user_account = request.GET['account']
password = request.GET['password']
username = account.objects.get(blog_account = user_account).blog_username #在数据库account表中获取账号对应的用户名(昵称)
user = users.objects.get(blog_username = username) #在users表中获取所有信息
context = {'user':user}
return render(request, 'index.html', context) #在index.html中显示信息

4、修改路径urls.py

from django.contrib import admin
from django.urls import path
from App import views
urlpatterns = [
path('admin/', admin.site.urls),
path(r'index/',views.index),
path(r'londing_form/',views.londing_form),
path(r'londing/',views.londing),
]

三、GET请求测试

上面的代码都是以get请求写的直接开启服务器: python manage.py runserver

四、POST请求测试

1、修改landing.html  注意:action部分相比get请求结尾多了一个“/”

<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
</head>
<body >
<form action="/londing/" method="post">
{% csrf_token %} csrf 全称是 Cross Site Request Forgery。这是Django提供的防止伪装提交请求的功能。POST 方法提交的表格,必须有此标签。
账号:<input type="text" name="account">
密码:<input type="text" name="password">
<input type="submit" value="登陆">
</form>
</body>
</html>

2、修改views.py中的londing函数

def londing(request):
if request.POST:
user_account = request.POST['account']
password = request.POST['password']
username = account.objects.get(blog_account = user_account).blog_username
user = users.objects.get(blog_username = username)
context = {'user':user}
return render(request, 'index.html', context)

3、测试

【Django笔记四】Django2.0中的表单的更多相关文章

  1. angular4.0中form表单双向数据绑定正确姿势

    issue:用[(ngModel)]="property"指令双向数据绑定,报错. reason1:使用ngModel绑定数据需要注入FormsModule模块,在app.modu ...

  2. django中form表单的提交:

    一,关于表单: 表单在百度百科的解释:   表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...

  3. Django2.0中文文档

    title: Django2.0中文文档 tags: Python,Django,入沐三分 grammar_cjkRuby: true --- Django2.0版本已经发布了,我们先来看一个图片 从 ...

  4. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  5. Django2.0中得url路由path得用法

    Django2.0中,url得匹配规则更新了,在django1.0中,url是用正则表达式书写得,相对来说比较繁琐一些,在django2.0中进行了升级优化,改为了path from django.u ...

  6. python2.X现在不能安装Django了:Collecting django Using cached Django-2.0.tar.gz

    使用pip安装django2: pip install django 报错: Collecting django  Using cached Django-2.0.tar.gz    Complete ...

  7. Django中的表单

    目录 表单 Django中的表单 用表单验证数据 自定义验证 表单 HTML中的表单是用来提交数据给服务器的,不管后台服务器用的是 Django  还是 PHP还是JSP还是其他语言.只要把 inpu ...

  8. Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法

    Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法 一丶ORM常用字段 ...

  9. JavaScript中的表单编程

    表单编程 1获取表单相关信息 1.什么是表单元素 1.什么是表单元素 在H TML中表单就是指form标签,它的作用是将用户输入或选择的数据提交给指定的服务器 2.如何获取表单元素 <form ...

随机推荐

  1. localstorage本地存储的简单使用

    我们在做页面时会用到本地存储的时候,今天说说localStorage本地存储. 1.localStorage.name="老王";      //第一种设置存储本地数据的方法loc ...

  2. 2-4 Sass的函数功能-颜色函数

    RGB颜色函数-RGB()颜色函数 在 Sass 的官方文档中,列出了 Sass 的颜色函数清单,从大的方面主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数, ...

  3. jQuery对html元素取值与赋值

    以下总结了常用的jQuery选择器对html元素取值与赋值 Textbox:  var str = $('#txt').val(); $('#txt').val("Set Lbl Value ...

  4. 【One Day菜鸟到大鸟】MyBatis搭建环境

    一.概述     MyBatis是一个持久化框架和Hiberante差不多.比起JDBC来说MyBatis封装了JDBC让我们能够面向对象开发.比起Hiberante来说卸下了Hiberante那种重 ...

  5. .Net Core 初体验及总结(内含命令大全)

    dotnet 命令目录: dotnet new  -创建 dotnet restore  -还原 dotnet build -编译 dotnet run -运行 dotnet test -测试 dot ...

  6. Django后台注册

  7. Php 性能参数优化 及 Iptables 防火墙限制用户访问平率

    Php-Fpm.Conf 文件配置优化 [global] pid = run/php-fpm.pid process_control_timeout=5 [www] listen.allowed_cl ...

  8. TreeSet 题

    QUESTION 4Given:12. import java.util.*;13. public class Explorer2 {14. public static void main(Strin ...

  9. Net编译原理简单

    转载:http://blog.csdn.net/sundacheng1989/article/details/20941893 首先简单说一下计算机软件运行.所谓软件运行,就是一步一步做一些事情.计算 ...

  10. Linux文件的I/O操作

    C标准函数与系统函数的区别   标准函数printf调用应用层api,然后应用层api调用内核层api,再通过内核层api调用硬件设备   一个pirntf打印helloworld那么sys_writ ...