我需要在我的html表单的子目录上指向一个空白的html页面(test_page1.html)。我怎么能在Django中做到这一点?
我的urls.py文件中没有任何新页面的映射。
test_page.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
</head>
<body>
This is a test page
{% if display_form %}
<form action="test_page1.html" method="post">{% csrf_token %}
FIRST NAME : <input type="text" name="fname">
<input type="submit" value="register"/>
</form>
{% else %}
{% autoescape off %}
{{ firstname }}
{% endautoescape %}
{% endif %}
</body>
</html>
views.py
def test_page(request):
if request.method == 'POST':
print 'request.post = ', request.POST['fname']
fname = cgi.escape(request.POST['fname'])
print 'fname =', fname
variables = RequestContext(request,{'display_form':False,'firstname':fname})
return render_to_response('test_page.html',variables)
else:
variables = RequestContext(request,{'display_form':True})
return render_to_response('test_page.html',variables)
答案 0 :(得分:0)
django文档中有一个示例:https://docs.djangoproject.com/en/1.6/topics/forms/#using-a-form-in-a-view
您需要重定向响应而不是render_to_response:
from django.http import HttpResponseRedirect
你的函数的post部分应该是这样的:
if request.method == 'POST':
# Your custom processing.
if everything_is_alright:
return HttpResponseRedirect('/the_blank_page/')
# Not everything is right, so re-render the original page with the form.
# Put your custom render_to_response() stuff here.
请注意,使用Django的实际表单机制可能会有所帮助。 Django为你做了cgi.escape
所有的事情加上许多额外的安全功能。