Django - How can I pass a string from a URL to a view ?

Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET.


With this method the URLConf file is configured to define your URL parameter via the use of regex.

Say that we have a URL that is '' and we want to grab 'value'.
First we would configure our with,

url(r'^user=(?P<user>\d{5})$', 'domain.views.output'),

This "value" can then be passed to a function within your view. Such as,

def output(request, user):


The other way of achieving this is via the use of the request.GET method which is typically used when your url contains a question mark, such as ''.

First of all your is configured with a standard entry.

url(r'^$', 'domain.views.output'),

We then use the request.GET to assign the "value" to the variable x.

def output(request):
   x = request.GET.get('user', '')

Tags: Django

About the Author


R Donato

Rick Donato is the Founder and Chief Editor of He currently works as an SDN/NFV Solutions Architect and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001