SoFunction
Updated on 2024-11-18

Python Django Getting Data from URLs Explained

Django Getting Data from a URL

Parameters in URLs generally take two forms. These are shown below:

1. /article/details/120816954
2. /so/search?q=Django&t=blog&u=zy010101

The first form is called "URL path parameter"; the second form is called "URL keyword form". Here is how to get these two forms of data in Django.

URL path parameters

Using the path function

from  import path
from . import views
urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    path('articles/<int:year>/', views.year_archive),
    path('articles/<int:year>/<int:month>/', views.month_archive),
    path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]

For an explanatory note on this URL, refer directly to theDjango official textJust file it.

In case I can't get into the documentation sometimes, I've posted the official documentation directly below as well:

在这里插入图片描述

Using the re_path function

If, using the path function doesn't satisfy you to match the URL, then you can use the re_path function to use regular expressions to match the parameters in the URL path. Note that in Django, the syntax for using regular expressions to get values in groups is(?P<name>pattern), where name is the group name and pattern is the pattern to match.

from  import path, re_path
from . import views
urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]

This description of the URL configuration can be found inUsing regular expressions

Again, in case you can't get into the documentation sometimes, I've posted the official documentation directly below as well:

在这里插入图片描述

It is important to note that after the regular expression is matched, the captured parameters are passed as strings to the view function (view class).

URL keyword form

Usually, in addition to passing data in the URL path, you can also pass data in the URL parameters. Example:

/index?keys=123&values=qwe

This URL passes the parameters keys and values, whose values are 123, qwe.

Before that, let's go over the pre-conditionsQueryDict。

HttpRequest object properties GET, POST are QueryDict type objects

Django get URL keyword parameters can be obtained through attributes. Example:

def test(request):
    a = ("a")
    b = ("b")
    c = ("c")
    a_all = ("a")
    res = a+'<br>'+b+'<br>'+c+'<br>'+str(a_all)
    return HttpResponse(res)

Now make the request using the following URL:

http://127.0.0.1:8008/test?a=1&a=2&b=3&c=4

The page is displayed as shown below:

在这里插入图片描述

The query string does not distinguish between request methods, i.e., if the client makes a POST request, it can still obtain the query string data in the request.

summarize

That's all for this post, I hope it helped you and I hope you'll check back for more from me!