djangodjango-modelsdjango-database

"Returning to that page might cause any action you took to be repeated" - Django


I have a form on my website, that creates an entry in database. So every time when I refresh a page I got this message first:

The page that you're looking for used information that you entered.
Returning to that page might cause any action you took to be repeated.
Do you want to continue?

Obviously I don't want have the same information more than once in my database.

just in case: this is my code (I know there is a lot of crap that needs to be deleted):

#views.py
@login_required
def subject(request,username, subject_name):
    subject_id = Subjects.objects.filter(user = request.user).get(name=subject_name)
    #Upload form
    if request.method == "POST":
        if "upload-b" in request.POST:
            form = ContentForm(request.POST, request.FILES, instance=subject_id)       
            if form.is_valid(): # need to add some clean functions
                 up_f = FileDescription.objects.get_or_create(subject=subject_id,
                                                  subject_name=subject_name,
                                                  file_type=request.POST['file_type'],
                                                  file_uploaded_by = username,
                                                  file_name=request.POST['file_name'],
                                                  file_description=request.POST['file_description'],
                                                  image = request.FILES['image'],
                                                  )
form = ContentForm()

#Show uploaded files with respect to clicked session (Homework, Class , Random ... )
homework_files = Homework.homework.filter(subject_name__exact=subject_name,
                                         file_uploaded_by__exact=username)
class_files = ClassPapers.classpapers.filter(subject_name__exact=subject_name)
random_files = RandomPapers.randompapers.filter(subject_name__exact=subject_name,
                                           file_uploaded_by__exact=username)




return render_to_response('subject_content.html', {'form':form,
                                                   'subject_name': subject_name,
                                                   'class_files': class_files,
                                                   'homework_files': homework_files,
                                                   'class_files': class_files,
                                                   'random_files': random_files,
                                                   },
                           context_instance=RequestContext(request))


#forms.py:
class ContentForm(forms.ModelForm):
    file_name =forms.CharField(max_length=255, widget=forms.TextInput(attrs={'size':20}))
    file_description = forms.CharField(widget=forms.Textarea(attrs={'rows':4, 'cols':25}))
    class Meta:
        model = FileDescription 
        exclude = ('subject', 'subject_name', 'file_uploaded_by')


#template
    <div id="sbj-creation-frm">
        <h3>Upload File</h3>
        <form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="submit" name="upload-b" class="btn-create" />
        </form>
    </div>

Solution

  • This message is from the browser; and it will display anytime you try to refresh a page that was displayed as the result of a POST request.

    It has no bearing on your code, the browser will display the same message on all websites where you try to refresh the page (hit F5 for example) which was displayed as a result of a previous POST request.

    To prevent this from happening, make sure all POST requests redirect to a different view upon completion; and not render templates themselves.