Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET.
URLConf
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 ‘www.domain.co.uk/user=value’ and we want to grab ‘value’.
First we would configure our urls.py 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):
request.GET
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 ‘www.domain.co.uk/?user=value’.
First of all your urls.py 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', '')
- How to Configure a BIND Server on Ubuntu - March 15, 2018
- What is a BGP Confederation? - March 6, 2018
- Cisco – What is BGP ORF (Outbound Route Filtering)? - March 5, 2018
Want to become a Django expert?
Here is our hand-picked selection of the best courses you can find online:
The Complete Web Development Bootcamp course
Django Practical Guide course
Django Full Stack Developer Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial