标签:
修改alu02/urls.py
urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^blog/register$‘, ‘blog.views.register‘), ]
修改blog/views.py
[root@host-100-100-5-17 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}) [root@host-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>
启动
[root@host-100-100-5-17 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‘, ]
重试
标签:
原文地址:http://www.cnblogs.com/onmyway227/p/5252745.html