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

How to Retrieve GET Request Values in Django?

Linda Hamilton
Linda HamiltonOriginal
2024-11-28 21:12:161001browse

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')

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!

Statement:
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