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

简单配置路由:

简单表单提交:

<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. python之文件目录和路径

    1.路径中不要出现中文,否则有极大可能报错 2.反斜杠问题 举例说明: 我们从Windows复制的文件路径是G:\beifen\Tea. 可以看到,路径用的是反斜杠:\. 由于反斜杠\在python里 ...

  2. python中,如何将多行进行输出,同时将行尾的换行符去掉

    需求说明: 比如我要输出字符串的常量,字符串常量要输出多行,该怎么解决呢 操作过程: 1.可以通过三引号(""" .... """)将要输出 ...

  3. 安装office2010出现了错误,提示要安装MSXML6.10.1129.0解决方法

    将下面的内容复制到记事本中,然后将记事本存成.reg文件 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\TypeLib\{F5078F1 ...

  4. SpringBoot------thymeleaf的使用

    1.pom.xml添加相应依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  5. passport登录问题:passport.use 方法没有被调用

    写passport登录验证时,无论如何passport.use 方法都没有被调用,最后在同事的帮助下,才找到问题: 我是用form提交登陆数据的, input type:"text" ...

  6. Cesium加载影像和地形数据+开启高程遮挡效果+视点定位+定时更新

    // 初始化Cesium var viewer = new Cesium.Viewer('cesiumContainer', { /*imageryProvider : new Cesium.ArcG ...

  7. osgEarth设置模型旋转角度

    #include<windows.h> #include <osgViewer/Viewer> #include <osgEarthDrivers/gdal/GDALOp ...

  8. U3D各键值说明

    KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键. 值        对应键 Backspace     退格键 Delete      Delete ...

  9. 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu

    环境:VMWare Player 5.0.2,Ubuntu 13.10  VMWare共有3种网络连接模式,分别是: 1. bridged(桥接模式):虚拟机将直接连接到物理局域网,使自身独立于宿主机 ...

  10. linux sumba服务器简单配置

    使用samba设置linux和windows直接简单的文件共享 前提: 1.linux和windows已经可以互相ping同 2.已经安装好smb 查看是否安装smb rpm -aq|grep smb ...