[Fixed]-Python Djando easiest way to display url parameters

1👍

You can send that value in your context to the view.

Given an url like this:

urls.py

...
url(r'^item/(?P<item_code>[0-9A-Z]+)', item_view, name="url_name")
...

And a view like this:

views.py

def item_view(request, item_code):
    ...  # Do something here
    context = {'item_code': item_code, }
    return render(request, 'your_template.html', context=context)

And then in your template you can use:

your_template.html

<p>this page is for {{ item_code }}</p>
👤Gocht

0👍

In my opinion, the easiest way is to just use GET variables:

URL: http://example.com/item?code=ABC1234

def item(request):
    return HttpResponse("this is page for %s" % request.GET.get('code', 'default')

It’s quite flexible since you can add and use more variables without modifying your url pattern.

However this is not the prettiest method.

👤Peter

Leave a comment