表单提交可能会报错,注意一行代码就可以解决:

简单配置路由:

简单表单提交:

<form action="/index/" method="post">
<p><input type="text" name="username" placeholder="请输入用户名"></p>
<p><input type="text" name="password" placeholder="请输入密码"></p>
<p><input type="submit" value="提交"></p>
</form>

可见上面的提交还是提交到当前方法:可以通过判断提交方式来判断是提交表单还是请求页面:

def index(request):
#return HttpResponse("hello word")
if request.method == 'POST':
u = request.POST['username']
p = request.POST['password']
print(u,p)
return render(request,'index.html')

这种接受如果,接受不到就会报错,所以都是这样接受的:

def index(request):
#return HttpResponse("hello word")
if request.method == 'POST':
u = request.POST.get('username',None)
p = request.POST.get('password',None)
print(u,p)
if u =='' and p == '':
from django.shortcuts import redirect
return redirect("http://www.baidu.com")
return render(request,'index.html')

示例demo:

from django.shortcuts import render
from django.shortcuts import HttpResponse
#coding=utf-8
def index(request):
#return HttpResponse("hello word")
error_msg = ''
if request.method == 'POST':
u = request.POST.get('username',None)
p = request.POST.get('password',None)
print(u,p)
if u =='' and p == '':
from django.shortcuts import redirect
return redirect("/index/")
else:
error_msg = "failse to login"
return render(request,'index.html',{'error_msg':error_msg})

模板:

<form action="/index/" method="post">
<p><input type="text" name="username" placeholder="请输入用户名"></p>
<p><input type="text" name="password" placeholder="请输入密码"></p>
<p><input type="submit" value="提交">{{ error_msg }}</p>
</form>

Django---简单from表单提交的更多相关文章

  1. Maven web项目(简单的表单提交) 搭建(eclipse)

    我们将会搭建一个,基于Maven管理的,具有简单的表单提交功能的web项目,使用DAO--service--WEB三层结构,服务器使用Tomcat 1 项目基本结构的搭建 左上角File---> ...

  2. jQuery最简单的表单提交方式

    第一步:绑定事件 常用的与ajax相关的事件参考如下: 1.$(selector).click(function) 2.$(selector).change(function) 3.$(selecto ...

  3. Django之form表单提交并验证

    1.提交的时候会报错 2. 需要在setting里面注释掉一句话,关闭跨站请求检查. 3. 注释掉以后,理论上就不报错了.可我还是卡壳了. 4. 通过在网上找方法,修复错误. 原因:表单action字 ...

  4. Django 使用form表单提交数据报错: Forbidden (403)

    Issue: 使用from表单submit之后报错入下: Action: 把django工程文件的setting.py中的'django.middleware.csrf.CsrfViewMiddlew ...

  5. Konckout开发实例:简单的表单提交页面

    <!doctype html> <html > <head> <meta http-equiv="Content-Type" conten ...

  6. PHP——简单的表单提交

    <body> <form name="" method="post" action="CHULI.php"> < ...

  7. springmvc 表单提交

    Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...

  8. [Spring MVC] - 表单提交

    Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...

  9. (转)ASP.NET MVC 第五个预览版和表单提交场景

    转自:http://ourlife.blog.51cto.com/708821/296171 上个星期四,ASP.NET MVC开发团队发布了ASP.NET MVC框架的“第五个预览版”.你可以在这里 ...

  10. 代码段:js表单提交检测

    市面上当然有很多成型的框架,比如jquery的validation插件各种吧.现在工作地,由于前端童鞋也没用这些个插件.根据业务的要求,自己就在代码里写了个简单的表单提交的检测代码(php的也写了一个 ...

随机推荐

  1. 修改linux终端DIR显示颜色

    头疼死,linux终端下,目录颜色蓝色在黑色的背景下,睁大双眼都看不清楚. 找办法修改,找到默认的颜色设置目录: # vi /etc/DIR_COLORS 查看文件,并查找DIR: 可以看到默认设定“ ...

  2. ioncube

    FileRun多功能的VPS文件管理器使用ioncube加密的 ioncube通过将代码编译成字节码,使PHP源代码免受他人监控.剽窃以及改动 ioncube提供了一个安装的向导程序 下载地址http ...

  3. PHP 使用redis

    <?php /*从平台获取数据库名*/ $dbname = ""; /*从环境变量里取host,port,user,pwd*/ $host = ''; $port = ''; ...

  4. 跨域、跨服务器调用时候session丢失的问题

    最近新进一个公司,做的项目是手机支付系统.由于涉及到金钱相关的,所以安全性要求特别的高.项目分了很多个子系统,在部署(测试)的时候是每个Tomcat上面只放一个子系统.比如现在有5个子系统,那么就会对 ...

  5. linux中高亮显示文本的工具 -- bat

    bat 的项目地址 https://github.com/sharkdp/bat bat 是用rust 开发的, 在centos中安装bat需要rust的环境, 我们可以通过安装rust的包管理工具c ...

  6. 一些JavaScript基本函数

    1.document.write(”");为 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4.一个浏览 ...

  7. SpringBoot------集成PageHelper分页功能

    添加MyBatis的代码,地址 https://www.cnblogs.com/tianhengblogs/p/9537665.html 修改以下部分: 1.添加MyBatisConfig packa ...

  8. Hibernate_day03讲义_使用Hibernate完成一对多的关系映射并操作

  9. 【GIS】ArcGIS JS 4.X

    require(["esri/Map", "esri/views/SceneView", "esri/TileLayer/TdtMapLayer/Td ...

  10. MySQL复制搭建

    1. 原理 参考<涂抹MySQL  跟着三思一步一步学MySQL >这本书. 2.环境背景 操作系统 :CentOS 6.5 数据库版本:MySQL 5.6 主库A:192.168.1.2 ...