Home > Backend Development > Python Tutorial > How to Retrieve GET Request Values in Django?

How to Retrieve GET Request Values in Django?

Linda Hamilton
Release: 2024-11-28 21:12:16
Original
1073 people have browsed it

How to Retrieve GET Request Values in Django?

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')
Copy after login

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,),
Copy after login

In your views.py:

def profile_page(request, username):
    # Rest of the view function code
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template