Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing URL parameters in request.GET

Tags:

rest

url

django

I am currently defining regular expressions in order to capture parameters in a URL, as described in the tutorial. How do I access parameters from the URL as part the HttpRequest object?

My HttpRequest.GET currently returns an empty QueryDict object.

I'd like to learn how to do this without a library, so I can get to know Django better.

like image 882
sutee Avatar asked Sep 29 '08 20:09

sutee


People also ask

Can we pass parameters in GET request?

In a GET request, you pass parameters as part of the query string.

How do I get parameters in GET request?

GET parameters (also called URL parameters or query strings) are used when a client, such as a browser, requests a particular resource from a web server using the HTTP protocol. These parameters are usually name-value pairs, separated by an equals sign = . They can be used for a variety of things, as explained below.

How do you pass string parameters in GET request URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


1 Answers

When a URL is like domain/search/?q=haha, you would use request.GET.get('q', '').

q is the parameter you want, and '' is the default value if q isn't found.

However, if you are instead just configuring your URLconf**, then your captures from the regex are passed to the function as arguments (or named arguments).

Such as:

(r'^user/(?P<username>\w{0,50})/$', views.profile_page,), 

Then in your views.py you would have

def profile_page(request, username):     # Rest of the method 
like image 101
camflan Avatar answered Sep 28 '22 23:09

camflan