Django, rendering a view from another view (call a specific URL)

10,853

You should always redirect, not render, after a successful post. Doing return redirect(url) is the correct thing to do, and it's not clear why you're not happy with this.

Note that redirect can accept the name of a URL pattern, so you could do return redirect('userfilt') which will take you to the correct place.

Share:
10,853
ivoruJavaBoy
Author by

ivoruJavaBoy

Updated on June 05, 2022

Comments

  • ivoruJavaBoy
    ivoruJavaBoy almost 2 years

    I have a little form:

    <form action="#" method="post">
        {% csrf_token %}
        <label>Company Number:</label>
        <input type="text" name="company" placeholder=""/><br>
        <input type="submit" id="register value=" OK" />
    </form>
    

    Which is mapped like this:

    url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm'),
    

    Now after submitting this form, I want to get back to the main view:

    url(r'^userfilt/$', views.userfilt, name='userfilt')
    

    URL mapping file:

    app_name = 'SSO_Management_POC'
    urlpatterns = [
        url(r'^$', views.IndexView.as_view(), name='index'),
        url(r'^user/$', views.user, name='user'),
        url(r'^userfilt/$', views.userfilt, name='userfilt'),
        url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm'),
        #url(r'^updateForm/$', views.updateForm, name='updateForm'),
        url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
        url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    ]
    

    The main view is like this (but I don't think this code is related to the problem..):

    def userfilt(request):
        if request.GET.get('create'):
            return HttpResponseRedirect('insertForm')
    
        if request.GET.get('update'):
            print request.GET.get('pk')
            return render(request, 'SSO_Management_POC/updateForm.html')
    
        if request.method == 'POST':
            form = UserForm(request.POST)
            val = request.POST.get('my_textarea')
            return render(request, 'SSO_Management_POC/userfilt.html',
                          {'top_user': TopUser.objects.filter(user_name__regex=val)})
        else:
            print '4'
            return render(request, 'SSO_Management_POC/userfilt.html')
    

    Now the call that is killing me happens when N submit the form, N just wanna get back to the main page calling it with a POST, like a always did!

    return render(request, "SSO_Management_POC/userfilt.html")
    

    I do it like this, but the problem is that the URL has not been reset.. and results in this,

    http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/#

    Resulting in every operation I make on that page not work because it's not mapped anymore

    I mean, it should be:

    but instead it is

    To try to explain my issue better..

    I go to the main page (http://127.0.0.1:8000/SSO_Management_POC/userfilt/)

    "GET /SSO_Management_POC/userfilt/ HTTP/1.1" 200 2648

    Then I click on the create User to call the form, it brings me to: (http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/)

    "GET /SSO_Management_POC/userfilt/insertForm/ HTTP/1.1" 200 1549

    Than I submit the form and I would like to get back to the main page, and here comes the thing I don't understand: it bring me here

    http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/#

    But I want to go here

    http://127.0.0.1:8000/SSO_Management_POC/userfilt

    This is again the code:

    return render(request, "SSO_Management_POC/userfilt.html")
    

    This is the call made:

    "POST /SSO_Management_POC/userfilt/insertForm/ HTTP/1.1" 200 2648

    I tried with render, with HttpResponseRedirect and so on... But always it happened the URL I'm calling with the previous one, I want it to be reset, I want it to be ../ !!!

    The only thing that work for me is:

    return redirect("../")
    

    but

    1. this is dirty
    2. this does not permit to make a POST call!

    Thanks to Daniel, I fixed it like this:

    return redirect('/SSO_Management_POC/userfilt')
    

    But it still give me same issue with:

    return render(request,'/SSO_Management_POC/userfilt')
    

    Getting me to http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/

    • raphv
      raphv almost 8 years
      I'm not sure I understand your question... If you want your form to redirect from URL A to URL B, you either have to make URL B the destination (action="...") of your form (in which case, view B has to process the form) OR use post to A and redirect to B, but you CANNOT have URL B loaded with a POST request. Which option would you prefer?
    • ivoruJavaBoy
      ivoruJavaBoy almost 8 years
      If i use the action it always bring me to 127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/… instead of /SSO_Management_POC/userfilt.html it always append my text to the last page i was, the form one!
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    No :( if i do like this it always bring me to 127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/… instead of /SSO_Management_POC/userfilt.html
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    And i want to call the form method "def insertForm(request)" via url mapping, and it works, it's just the redirection that i make at the end of this one, that wont work, cause it append me the url requested to the previous one...
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    No it does not! If i do like this: redirect('userfilt') it gives me this error: Reverse for 'userfilt' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] and if i try (like always done) like this: return redirect('SSO_Management_POC/userfilt.html') (so clear, so easy, should be...) but it brings me here: 127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/…
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    The second case is the one that i don't understand, if i do return redirect('SSO_Management_POC/userfilt.html') why it brings me to 127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/… ??? appending that url to the previous one...
  • Daniel Roseman
    Daniel Roseman almost 8 years
    Because you haven't started from the root, by using a leading slash: redirect("/...") The first error is presumably because the userfilt pattern is included inside a namespace, you haven't given enough information to know exactly what.
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    I added all the url mapping py file, at first look to me, that i'm not an expert, they seem to be right...
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    Ok Daniel, how I don't know where are you, but if you want i can buy you a ticket to get here and give me some punches on my empty head... i think it was all about LEADING SLASH... :( :(
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    BUT MAIN QUESTION: why doing: return redirect('/SSO_Management_POC/userfilt') its ok!! and doing return render(request, '/SSO_Management_POC/userfilt') it gets me always to 127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm ???
  • Daniel Roseman
    Daniel Roseman almost 8 years
    Because that's the URL you're posting to. render doesn't change the URL.
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    And is there any way to render to the previous one, i mean to an URL that does not consider the one you are coming from?
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    Is there any way to render to the 127.0.0.1:8000/SSO_Management_POC/userfilt? Or maybe just calling this url with a post from the other view?
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    I just want to call it with the POST method from the other view... :( should not be so hard..
  • Daniel Roseman
    Daniel Roseman almost 8 years
    Argh, yes: with redirect! This really isn't hard. Just redirect. That's all.
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    Sorry Daniel, be patiance :( with the redirect its making a get call: GET /SSO_Management_POC/userfilt/?label=User%20Registered! HTTP/1.1" 200 2834
  • ivoruJavaBoy
    ivoruJavaBoy almost 8 years
    and before i read this question where was talking about this too: stackoverflow.com/questions/3024168/…