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

4

18

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>
Nolasco answered 4/8, 2012 at 19:22 Comment(0)
V
35

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.

Voltcoulomb answered 4/8, 2012 at 19:25 Comment(5)
Thank you, very much. But is there any ways to avoid it?Nolasco
Yes, POST requests should issue a redirect once finished, which will prevent this error from displaying.Voltcoulomb
Yet another way to prevent it: #45656905Macaluso
Yes, If it is ASP.net MVC, use return RedirectToAction("Index"); Reference to [link]https://mcmap.net/q/669895/-aspx-page-response-redirect-to-mvc-viewBilli
I changed return render to return redirect to the same page, and now it went away, and I don't need to use context to the post function anymore.Encumbrancer
M
2

redirect to same page working for me :

header("Location: #");
Musicology answered 28/3, 2019 at 15:9 Comment(0)
C
0

Just redirect your page to current page after inserting , it will clear all the values and avoid adding the Duplicate records !

example:

protected void btnAdd_Click(object sender, EventArgs e)
{
 //your code 
 Response.Redirect("Currentpage.aspx",true);

 //or 
 Response.Redirect(Request.Url.AbsoluteUri);
}
Custos answered 7/4, 2014 at 13:24 Comment(2)
I get the error Use of undefined constant Response - assumed 'Response'Katmandu
Because the question is for Django, and this answer is for...is that .net?Esther
L
0

Try this JS:

<script type="text/javascript">
  if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
  }
</script>
Livingstone answered 30/5 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.