2

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/

pppery
  • 3,731
  • 22
  • 33
  • 46
ivoruJavaBoy
  • 1,307
  • 2
  • 19
  • 39
  • 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? – raphv Jun 30 '16 at 13:31
  • If i use the action it always bring me to http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/userfilt.html instead of /SSO_Management_POC/userfilt.html it always append my text to the last page i was, the form one! – ivoruJavaBoy Jun 30 '16 at 13:36

2 Answers2

3

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 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: http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/SSO_Management_POC/userfilt.html – ivoruJavaBoy Jun 30 '16 at 13:45
  • 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 http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/SSO_Management_POC/userfilt.html ??? appending that url to the previous one... – ivoruJavaBoy Jun 30 '16 at 13:49
  • 1
    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. – Daniel Roseman Jun 30 '16 at 13:51
  • 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 Jun 30 '16 at 14:09
  • 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 Jun 30 '16 at 14:12
  • 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 http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/ ??? – ivoruJavaBoy Jun 30 '16 at 14:15
  • Because that's the URL you're posting to. `render` doesn't change the URL. – Daniel Roseman Jun 30 '16 at 14:17
  • 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 Jun 30 '16 at 14:20
  • Is there any way to render to the http://127.0.0.1:8000/SSO_Management_POC/userfilt/? Or maybe just calling this url with a post from the other view? – ivoruJavaBoy Jun 30 '16 at 14:21
  • I just want to call it with the POST method from the other view... :( should not be so hard.. – ivoruJavaBoy Jun 30 '16 at 14:23
  • Argh, yes: with redirect! This really *isn't* hard. Just redirect. That's all. – Daniel Roseman Jun 30 '16 at 14:32
  • 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 Jun 30 '16 at 14:36
  • and before i read this question where was talking about this too: http://stackoverflow.com/questions/3024168/django-how-do-i-redirect-a-post-and-pass-on-the-post-data – ivoruJavaBoy Jun 30 '16 at 14:37
0

Does the following code solve your problem?

In your template:

<form action="{% url 'userfilt' %}" method="post">
raphv
  • 1,153
  • 7
  • 10
  • No :( if i do like this it always bring me to http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/userfilt.html instead of /SSO_Management_POC/userfilt.html – ivoruJavaBoy Jun 30 '16 at 13:35
  • 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 Jun 30 '16 at 13:37