Retrieving GET Request Values in Django
When defining regular expressions to extract parameters from URLs according to the Django tutorial, it's common to encounter an empty QueryDict object in HttpRequest.GET. Here's how to access URL parameters through HttpRequest without relying on libraries.
Using HttpRequest.GET.get()
To retrieve a GET parameter named 'q' from the URL domain/search/?q=haha:
request.GET.get('q', 'default')
The 'default' parameter specifies the default value if 'q' is not found.
Accessing Parameters in URLConf
Alternatively, if you configure your URLConf to use regular expressions, the captured parameter values are passed as arguments (or named arguments) to the associated view function. For example:
(r'^user/(?P<username>\w{0,50})/$', views.profile_page,),
In your views.py:
def profile_page(request, username): # Rest of the view function code
The above is the detailed content of How to Retrieve GET Request Values in Django?. For more information, please follow other related articles on the PHP Chinese website!