ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 提交后的django clear form字段

2019-08-30 14:59:21  阅读:120  来源: 互联网

标签:python forms django


我有一个上传表单,在每个表单提交后,我想清除发布的数据,实际上表单是持有提交的数据.我知道,如果我将我的页面重定向到其他页面,这个问题可以解决,但我不知道我想重定向我的页面,因为在提交数据后,该页面中会显示成功消息.那么如何在不重定向页面的情况下清除表单?

这是我的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',}))

解决方法:

我已经解决了它.在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))

无需重定向您的页面.

标签:python,forms,django
来源: https://codeday.me/bug/20190830/1768876.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有