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

简单配置路由:

简单表单提交:

<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. 详解MathType中如何更改公式颜色

    在MathType数学公式编辑器中可以通过更改公式颜色,起到美化.标注公式的效果.本教程将详解MathType中如何更改公式颜色. 点击菜单栏中的样式->格式->颜色,用户就可以根据自己的 ...

  2. linux环境中,如何使用tar来创建压缩包?解压缩?

    需求说明: 今天需要将一个tomcat目录打成压缩包,使用zip感觉有点慢,所以就想用tar来试试,之前一直使用tar的解压缩命令, 今天试试tar的压缩命令 操作过程: 1.通过tar的zcf选项进 ...

  3. ios开发之--[_NSInlineData objectForKeyedSubscript:]

    reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fa2049 ...

  4. AcceptEx 以及 获取远程IP与port

    // 獲取本地以及遠程的IP和port setsockopt(clientfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&listenfd ...

  5. 【Cesium】坐标理解(转)

    https://blog.csdn.net/qq_34149805/article/details/78393540 1. 经纬度转换为世界坐标 第一种方式:直接转换: Cesium.Cartesia ...

  6. php 安装rabbitmq扩展无报错版

    需要安装rabbitmq-c,rabbitmq-c是一个用于C语言的,与AMQP server进行交互的client库.下载了v0.5.2版本(https://github.com/alanxz/ra ...

  7. 使用 Selenium

    Selenium 简介 Selenium 基本用法 Selenium 查找节点 Selenium 节点交互 Selenium 动作链 Selenium 执行 JavaScript Selenium 获 ...

  8. CentOS配制FTP服务器,并且能用root权限登录

    步骤如下: 1.运行yum install vsftpd命令 具体的细节如下:(如果无法更新,你先配置能访问互联网,我有文档叫 CentOS 在 VMware下,如何联网到Internet的解决办法可 ...

  9. XPath的初步认识

    嘿嘿,最近开始上班,不是过于太忙,而是自己一直在学习一些项目中用到的而我暂时还没接触的知识,WCF,log4等,感觉还没有总结的需要吧,虽然都了解啦,但是暂时还是初步的学习,基础的暂时是知道啦,还没有 ...

  10. MySql数据库设计表添加字段

    当要添加的字段属于整型,需要设置默认值 或者: alter table fp_user_base add hasPwd tinyint(4) not null default 0;