如何清除表单但仍显示其字段之一?
How to clear a form but still display one of its fields?
我想提交一个表单 F,清除它的字段,但仍然显示它的一个字段。 F 提交到一个视图,该视图呈现最先显示它的模板
为此,我 return 一个 HttpResponseRedirect
对象,而不是使用请求呈现模板:
def home(request):
if request.method == 'POST':
form = New_mini_url_form(request.POST)
if form.is_valid():
if (MiniUrl.objects.filter(url=form.cleaned_data['url']).count() == 0):
code = form.cleaned_data['code']
request.session["code"] = code
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
code = form.cleaned_data['code']
request.session["code"] = code
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
return render(request, 'mini_url/home.html', locals())
else:
form = New_mini_url_form()
return render(request, 'mini_url/home.html', locals())
我的模板中有 {{ request.session.code}}
这里是 project/settings.py
的摘录
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)
谢谢!
您可以在会话中设置 code
变量。然后在您的模板中使用 request.session.code
访问它。
def home(request):
if request.method == 'POST':
form = New_mini_url_form(request.POST)
if form.is_valid():
if (MiniUrl.objects.filter(url=form.cleaned_data['url']).count() == 0):
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
return render(request, 'mini_url/home.html', locals())
else:
form = New_mini_url_form()
return render(request, 'mini_url/home.html', locals())
然后在您的模板中,您可以通过以下方式访问 code
:
{{request.session.code}} # display code in the template
您必须在设置中包含 django.core.context_processors.request
。
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request', # will pass request in the template
...
)
来自 request context processor 上的 Django 1.7 文档:
django.core.context_processors.request
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
RequestContext will contain a variable request, which is the current
HttpRequest. Note that this processor is not enabled by default;
you’ll have to activate it.
我想提交一个表单 F,清除它的字段,但仍然显示它的一个字段。 F 提交到一个视图,该视图呈现最先显示它的模板
为此,我 return 一个 HttpResponseRedirect
对象,而不是使用请求呈现模板:
def home(request):
if request.method == 'POST':
form = New_mini_url_form(request.POST)
if form.is_valid():
if (MiniUrl.objects.filter(url=form.cleaned_data['url']).count() == 0):
code = form.cleaned_data['code']
request.session["code"] = code
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
code = form.cleaned_data['code']
request.session["code"] = code
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
return render(request, 'mini_url/home.html', locals())
else:
form = New_mini_url_form()
return render(request, 'mini_url/home.html', locals())
我的模板中有 {{ request.session.code}}
这里是 project/settings.py
的摘录TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)
谢谢!
您可以在会话中设置 code
变量。然后在您的模板中使用 request.session.code
访问它。
def home(request):
if request.method == 'POST':
form = New_mini_url_form(request.POST)
if form.is_valid():
if (MiniUrl.objects.filter(url=form.cleaned_data['url']).count() == 0):
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
code = form.cleaned_data['code']
request.session['code'] = code # set code in the session
[... stuff ...]
return HttpResponseRedirect(reverse(home))
else:
return render(request, 'mini_url/home.html', locals())
else:
form = New_mini_url_form()
return render(request, 'mini_url/home.html', locals())
然后在您的模板中,您可以通过以下方式访问 code
:
{{request.session.code}} # display code in the template
您必须在设置中包含 django.core.context_processors.request
。
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request', # will pass request in the template
...
)
来自 request context processor 上的 Django 1.7 文档:
django.core.context_processors.request
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you’ll have to activate it.