Django redirect to index view with correct URL after form submission

WonderSteve

I am learning Django and am trying to create a form that I can submit a participant's information to the database.

I have an index view, which list all the participants:

http://127.0.0.1:8000/participants/

Clicking a button on the index will go to form submission:

http://127.0.0.1:8000/participants/add_participant/

After submitting the form, the page goes back to the index view, but the URL is not correct, it stucks at http://127.0.0.1:8000/participants/add_participant/

If I refresh the browser immediately, it will add another record to the database.

add_participant.html

<!DOCTYPE html>
<html>
    <head>
        <title>This is the title</title>
    </head>

    <body>
        <h1>Add a Participant</h1>

        <form id="participant_form" method="post" action="/participants/add_participant/">

            {% csrf_token %}
            {{ form.as_p }}

            <input type="submit" name="submit" value="Create Participant" />
        </form>
    </body>

</html>

views.py

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect

from participants.models import Participant
from .forms import ParticipantForm


# Create your views here.
def index(request):
    participant_list = Participant.objects.order_by('-first_name')[:50]
    context = {'participants': participant_list}
    return render(request, 'participants/index.html', context)

def add_participant(request):
    if request.method == 'POST':
        form = ParticipantForm(request.POST)  
        if form.is_valid():
            form.save(commit=True) 
            return index(request)
    else:
        form = ParticipantForm() 


        return render(request, 'participants/add_participant.html', {'form': form})

urls.py

from django.conf.urls import url

from . import views
from .models import Participant

app_name = 'participants'

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'add_participant/$', views.add_participant, name='add_participant'),
]

I tried switching the

return index(request)

to:

return HttpResponseRedirect("http://127.0.0.1:8000/participants/")

It solves the problem...but I doubt this is the "right" way to do it. What is the correct way to fix this issue?

ahmed

You can pass just the path to the redirect response:

return HttpResponseRedirect("/participants/")

This way if you change your domain, the redirect will work.

an other solution is to use reverse

from django.core.urlresolvers import reverse
# ...
return HttpResponseRedirect(reverse(index))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect Url After Form Submission

From Dev

Not able to redirect a Django form after submission

From Dev

Redirect after AJAX form submission

From Dev

Cannot redirect after form submission

From Dev

redirect straight after form submission

From Dev

Redirect after AJAX form submission

From Dev

Run PHP Code After Form Submission And Redirect

From Dev

RoR: form_tag, redirect after submission

From Dev

How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

From Dev

How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

From Dev

Form submission using ajax and page view moderation after the submission

From Dev

Django form redirect after POST is unreliable and needed a success_url

From Dev

Django bootstrap modal after form submission

From Dev

Modify data after Django form submission

From Dev

Redirect to URL after form post

From Dev

Should I use a 301, 302, or 303 redirect after form submission?

From Dev

get redirect_to and flash notices to work after ajax form submission

From Dev

How to redirect to new html page after form submission in Apps script

From Dev

Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

From Dev

Missing Variable Error in Django Form After Form Submission

From Dev

Rails 4 - redirect to correct url after submit

From Dev

Rails 4 - redirect to correct url after submit

From Dev

Form submission unidentified index

From Dev

Redirect to other view after submitting form

From Dev

Redirect to other view after submitting form

From Dev

How to redirect page on form submission

From Dev

type object has no attribute error in django after form submission

From Dev

Simplify form submission in Django

From Dev

redirect url after selecting option in HTML form?

Related Related

  1. 1

    Redirect Url After Form Submission

  2. 2

    Not able to redirect a Django form after submission

  3. 3

    Redirect after AJAX form submission

  4. 4

    Cannot redirect after form submission

  5. 5

    redirect straight after form submission

  6. 6

    Redirect after AJAX form submission

  7. 7

    Run PHP Code After Form Submission And Redirect

  8. 8

    RoR: form_tag, redirect after submission

  9. 9

    How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

  10. 10

    How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

  11. 11

    Form submission using ajax and page view moderation after the submission

  12. 12

    Django form redirect after POST is unreliable and needed a success_url

  13. 13

    Django bootstrap modal after form submission

  14. 14

    Modify data after Django form submission

  15. 15

    Redirect to URL after form post

  16. 16

    Should I use a 301, 302, or 303 redirect after form submission?

  17. 17

    get redirect_to and flash notices to work after ajax form submission

  18. 18

    How to redirect to new html page after form submission in Apps script

  19. 19

    Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

  20. 20

    Missing Variable Error in Django Form After Form Submission

  21. 21

    Rails 4 - redirect to correct url after submit

  22. 22

    Rails 4 - redirect to correct url after submit

  23. 23

    Form submission unidentified index

  24. 24

    Redirect to other view after submitting form

  25. 25

    Redirect to other view after submitting form

  26. 26

    How to redirect page on form submission

  27. 27

    type object has no attribute error in django after form submission

  28. 28

    Simplify form submission in Django

  29. 29

    redirect url after selecting option in HTML form?

HotTag

Archive