form表单
Posted onmyway227
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了form表单相关的知识,希望对你有一定的参考价值。
修改alu02/urls.py
urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^blog/register$‘, ‘blog.views.register‘), ]
修改blog/views.py
[[email protected] alu02]# cat blog/views.py from django import forms from django.http import HttpResponse from django.shortcuts import render_to_response class UserForm(forms.Form): name = forms.CharField() def register(req): if req.method == ‘POST‘: form = UserForm(req.POST) if form.is_valid(): return HttpResponse(‘ok‘) else: form = UserForm() return render_to_response(‘register.html‘, {‘form‘:form}) [[email protected]-100-100-5-17 alu02]#
创建blog/templates/register.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <form method="POST"> {{form}} <input type=‘submit‘ value=‘ok‘/> </form> </body> </html>
启动
[[email protected] alu02]# python manage.py runserver
测试界面
点击‘ok‘,错误403发生
修改alu02/settings.py
MIDDLEWARE_CLASSES = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, #‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ]
重试
以上是关于form表单的主要内容,如果未能解决你的问题,请参考以下文章