我有一个上传表单,在每个表单提交后,我想清除发布的数据,实际上表单是持有提交的数据。我知道,如果我将页面重定向到其他页面,这个问题可以解决,但是我不想重定向我的页面,因为在提交数据后,该页面会显示成功消息。那么如何在不重定向页面的情况下清除表单?
这是我的views.py文件
def UserImageUpload(request):
if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
messages.add_message(request, messages.SUCCESS, 'Your Image upload is waiting for Admin approval')
newdoc = Photo(photo = request.FILES['photo'],watermarked_image=request.FILES['photo'],user = request.user,name = request.POST['name'],description = request.POST['description'],keyword = request.POST['Image_Keyword'],uploaded_time=datetime.datetime.now(),Certified=request.POST['Certification'])
newdoc.save()
else:
messages.add_message(request, messages.ERROR, 'Please Complete All Fields To Submit Your Image')
else:
form = DocumentForm()
uploaded_image = Photo.objects.all()
return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))
这是我的forms.py文件
from django import forms
class DocumentForm(forms.Form):
photo = forms.ImageField(
label='Select A file',)
name = forms.CharField(label='Image Name',max_length=50,widget=forms.TextInput(attrs={'class' : 'form-control',}))
Certification = forms.BooleanField(label='I certify that this is my original work and I am atlest 18 years of age')
description = forms.CharField(label='Image Description',max_length=500,widget=forms.TextInput(attrs={'class' : 'form-control',}))
Image_Keyword = forms.CharField(label='Keywords',widget=forms.TextInput(attrs={'class' : 'form-control',}))
答案 0 :(得分:3)
我已经解决了它。在views.py中,保存表单后只需指定空表单,就像那样
def UserImageUpload(request):
if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
messages.add_message(request, messages.SUCCESS, 'Your Image upload is waiting for Admin approval')
newdoc = Photo(photo = request.FILES['photo'],watermarked_image=request.FILES['photo'],user = request.user,name = request.POST['name'],description = request.POST['description'],keyword = request.POST['Image_Keyword'],uploaded_time=datetime.datetime.now(),Certified=request.POST['Certification'])
newdoc.save()
#Assign the empty form,it will empty the form after a successful form submission
form=DocumentForm()
else:
messages.add_message(request, messages.ERROR, 'Please Complete All Fields To Submit Your Image')
else:
form = DocumentForm()
uploaded_image = Photo.objects.all()
return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))
无需重定向您的网页。
答案 1 :(得分:2)
虽然Rego的答案在技术上是正确的,但Django最佳实践要求您在表单提交后执行HttpResponseRedirect。这减少了意外多次提交的可能性。
您有两种方法可以将数据发送到重定向的页面:您可以重定向到指示表单已成功提交的页面,和/或您可以使用会话变量。
http://docs.djangoproject.com/en/1.7/topics/http/sessions/
由于您无法将视图的本地变量发送到您重定向到的视图,因此您可以将该数据保存在会话变量中,这些变量可供任何后续会话使用,直到它过期或删除为止。
例如,您可以将用户的名称放在一个视图中的会话变量中:
# view.py (the one with the form)
request.session['name'] = form.cleaned_data['name']
然后在处理您的成功通知的视图中:
# view.py (form submission successful)
string = "Hi there, " + request.session['name'] + "! How are you today?"
这比Django众神强烈建议不做重定向更好。
答案 2 :(得分:1)
强烈建议在表单提交后进行重定向,以防止意外重复提交(请参阅Post/Redirect/Get)。
您可以使用Django的messages framework在重定向后显示消息。您认为:
from django.contrib import messages
from django.shortcuts import redirect, render
# ...
def UserImageUpload(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
# ... (do something with the submitted form data here) ...
# The message that will be shown on subsequent displays of the template:
messages.add_message(request,
messages.SUCCESS,
'Your Image upload is waiting for Admin approval')
# Redirect back to this view.
# Attention: Change '...' to the name or URL of this view.
return redirect('...')
else:
form = DocumentForm()
return render(request, 'myprofile/user_image_upload.html', {
'form': form,
'uploaded_image': Photo.objects.all(),
})
然后,您需要调整模板(即myprofile/user_image_upload.html
),以便能够在下一次渲染模板时显示该消息。将这样的内容添加到您的模板中:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
阅读reference,了解有关在模板中显示消息的更多详细信息。