1

I am using Django 1.3 with Python 2.7.2 on Windows 7 x64.

I have a URL pattern like this:

url(r'^(?P<club_id>\d+)/$', 'view_club')

From the pattern I want to be able to use request.GET in my templates to be able to create URLs to my views based on this GET variable (club_id). I have read online that I would be able to do:

{{ request.GET.club_id }}

However, this is always showing up as blank. I cannot figure out the reason why. I have the request context processor in my list of default context processors.

Thanks for the help.

2 Answers 2

1

In your example, club_id is not a GET parameter, it is part of the path in the URL. Your view function takes club_id as an argument, it should then put it in the context so the template can access it.

For example, your view function:

def view_club(request, club_id):
     return render(request, "template_name.html", { 'club_id': club_id })

then in your template:

{{ club_id }}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I literally posted mine at the same time. If I would have saw your answer first I wouldn't have posted : p
Is it possible to set up the URL so that I would be able to access it like I want? I don't want to have to edit 24+ views to include this in the context dict. I already thought of a context processor, but I don't think it's possible.
@Craysiii: you don't need to mention GET parameters in your url patterns at all. Change your code to use ?club_id=123, then your original code should work just fine.
0

IF you are passing an variable in the URL - in this case club_id - simply include it as an argument in your view function next to request:

def get_club(request,club_id):
     club = Club.object.get(pk=club_id)
     return render_to_response('club.html', {'club': club})

now in your template you can access {{club}} as the individually selected object and do things like {{club.name}} This also works for variables with multiple objects which you would use a for loop to iterate through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.