我正在学习一个教程,但无法理解其中的几行内容:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from . models import Page
from .forms import ContactForm
def index(request, pagename):
pagename = '/' + pagename
pg = get_object_or_404(Page, permalink=pagename)
context = {
'title': pg.title,
'content': pg.bodytext,
'last_updated': pg.update_date,
'page_list': Page.objects.all(),
}
# assert False
return render(request, 'pages/page.htm', context)
def contact(request):
submitted = False
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
#assert False
return HttpResponseRedirect('/contact?submitted=True')
else:
form = ContactForm()
if 'submitted' in request.GET:
submitted = True
return render(request,'pages/contact.htm',{'form': form, 'page_list': Page.objects.all(), 'sbmitted': submitted})
以上是pages/view.py
文件
{% extends "pages/page.htm" %}
{% block title %} Contact us {% endblock title %}
{% block content %}
<h1>Contact us</h1>
{% if submitted %}
<p class="success">
Your message was submitted successfully. Thankm you.
</p>
{% else %}
<form action="" method="post" novalidate>
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
{% csrf_token %}
</form>
{% endif %}
{% endblock content %}
以上是pages/contact.htm
文件
那么
是什么意思if requested.method == 'POST':
为什么要进行以下检查?
if submitted in request.GET:
submitted=True
答案 0 :(得分:0)
www.mydomain.com/Form?Key1=xxxxx&Key2=yyyyyyy
通过POST,数据被“隐藏”发送。例如,在登录表单中,您不希望密码在URL中可见。这就是为什么将这些形式用作发送POST的方法。
if request.method == 'POST':
验证发送的数据是否为POST格式
2。
else:
form = ContactForm()
if 'submitted' in request.GET:
submitted = True
这意味着,如果发送方法不是POST而是GET,请查看发送的信息中是否有submitted
参数,如果是,请将其值设置为True。
答案 1 :(得分:0)
应该有
如果request.GET.get('submitted')==“真”:
submitted = True
代替
if submitted in request.GET:
submitted=True