在我的主页面中,我添加了一个模板,其中包含用于向简报添加电子邮件的表单。我希望这个表单能够添加新的电子邮件,而无需在任何地方重定向。我怎么能做到这一点?这是我的代码,当我使用单独的页面时:
观点:
def newsletter_add(request):
if request.POST:
f = NewsletterForm(request.POST)
if f.is_valid():
try:
e = NewsletterEmails.objects.get(email = request.POST['email'])
msg = u'Mail already added'
return render_to_response('newsletter/add.html',
{'form':f,'msg':msg,},RequestContext(request))
except Address.DoesNotExist:
e = NewsletterEmails(email = request.POST['email'])
e.save()
msg = u'Mail added'
return HttpResponseRedirect('/')
else:
f = NewsletterForm()
return render_to_response('newsletter/add.html',{'form':f,},RequestContext(request))
表格:
{% block content %}
<div class="center-right">
<div class="pane profile">
<div class="div-content">
{{ msg }}
<form action="" method="post" enctype="multipart/form-data">
{{form.as_p}}
<input type="submit" class="submit" name="submit" value="Ok" />
</form>
</div>
</div>
</div>
{% endblock %}
形式:
class NewsletterEmails(forms.Form):
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,maxlength=75)),label=_("email"))
答案 0 :(得分:5)
没有重定向任何地方意味着
在案例-1中,您可以使用jquery-ajax-forms
执行此操作 在post-request将帖子请求重定向到同一页面而不是重定向到'/'之后的
return HttpResponseRedirect('redirect/to/form/page')