Django tutorial, HttpResponseRedirect, sending multiple arguments

1,249 views
Skip to first unread message

Ekberg Peter

unread,
May 7, 2020, 10:40:06 AM5/7/20
to Django users

I know something like this has been asked before but I never seen any answer that I can understand or that works. But I am very new on Django so maybe I could have missed it. But there must be an easy answer to my question. I am following the tutorial for Django and at section 4 they use the httpresponseredirect to go to next page after posting a form.


return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

The views page function looks like this:


def detail(request, question_id):

My question is: How can I send more arguments than the question.id to the function?

Thx

Andréas Kühne

unread,
May 7, 2020, 12:06:27 PM5/7/20
to django...@googlegroups.com
Hi Peter,

The thing is you need to handle this with your url files, your view function (or class) and your reverse call.

So for example:

urls.py

path('blog/<category:str>/<question_id:int>/', detail)

views.py:

def detail(request, category, question_id):

and finally the call to reverse:

reverse('polls:results', args=(category.slug, question.id, ))

Something like that.

All of this is connected - so you need to do all of it. However if you only want to get a url parameter, you can do so via the request object:

For example: 

blog/category_slug/23/?shoe_size=42

to get the shoe_sizer you get it in the view:

def detail(request, category, question_id):
     shoe_size = request.GET.get('shoe_size')


Hope this helps!

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5be13b82-36e5-4939-a822-a0b56b6dee66%40googlegroups.com.

Ekberg Peter

unread,
May 7, 2020, 2:30:38 PM5/7/20
to Django users

Hello Andréas and thank you for the answer. But unfortunately it doesn't seem to do what I want or I just don't get it.

What I really try to do is very simple, but I have been trying for two days now and feel I am close to give up on Django. Because if the simpliest and most common thing in webb application is this hard to understand than I must find something else. Passing arguments is the most common thng so I can't understand why Django doesn't offer any example. Anyway what I want to do is this.

I have this call:
return HttpResponseRedirect(reverse('bsapp:result', args=(question.id,)))

I suppose this must match This URL path:
path('<int:question_id>/result', views.result, name = 'result'),

That takes me to this function:
def detail(request, question_id):

From the
reverse call I want to include some extra arguments that I can use in the detail page.
In your example it seems going in wrong order from what I am asking for. You have the reverse last, but it is the reverse function that runs first.
But I would appreciate very much if you can make an example from my code here.


Thanks
Peter

Message has been deleted

Ekberg Peter

unread,
May 7, 2020, 3:07:29 PM5/7/20
to Django users
This is what I had to do. Thanks to you I know how to think and fixed it. 


xx
="EXTRA ARGUMENT" return HttpResponseRedirect(reverse('bsapp:result', args=(question.id, xx,)))
path('<int:question_id>/<str:xx>/result/', views.result, name='result'), def result(request, question_id, xx):

Andréas Kühne

unread,
May 7, 2020, 7:48:38 PM5/7/20
to django...@googlegroups.com
Hi,

Sorry for mixing those things up - I was writing from the top of my head and not really paying that much attention perhaps :)

I would like to give you a few other pointers:
1. This is a bit biased but I would try to get away from function based views - because they tend to make you do a lot of code duplication (in my view - and I know I am opinionated here).
2. I am not really sure that you should use URL based things in your case - but if you want to fine - I would probably use query params instead (and get it off the request object instead). 
3. Django IS really simple - probably one of the easiest things to create but complicated and simple websites in :)

I would recommend that you look to a good tutorial to get started - the django girls tutorial is one of the best: https://tutorial.djangogirls.org/en/

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Ekberg Peter

unread,
May 7, 2020, 9:05:49 PM5/7/20
to django...@googlegroups.com

Thank you for the feedback and advices. I will sure check out Django girls.
The mix of parameters was no problem, I figured that out. I was not sure though how to write the path. But now, at least I know how to send arguments and that's bothered me for a while. Now I am on the right track again. :-)

Peter




Andréas Kühne

unread,
May 8, 2020, 7:44:55 AM5/8/20
to django...@googlegroups.com
Cool!

Keep learning - I think that django is probably one of the best COMPLETE frameworks there is - And now shortly with async support, it will be even better :)

The way you can quickly prototype something that later on can continue into production is one of Pythons and Djangos strengths.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages