URL design and configuration

Django URL method path and _re_path for passing parameters

introduce

Django URLs provide two matching methods to pass parameters: path and re_path.

path is normal parameter passing,

re_path is a regular expression regex match.

path方法:采用双尖括号<变量类型:变量名>或<变量名>传递,例如<int:id>, <slug:slug>或<username>。

re_path方法: 采用命名组(?P<变量名>表达式)的方式传递参数。

 case

The following two ways of passing the article id to the view function are the same. The lowercase r before the quotation mark in re_path means that the quotation mark is a regular expression, please ignore the '\' and do not escape, ^ means the beginning, $ means the end, and \d+ means a positive integer.

# blog/urls.py
from django.urls import path, re_path

from . import views

urlpatterns = [
    path('blog/article/<int:id>/', views.article, name = 'article'),
   re_path(r'^blog/article/(?P<id>\d+)/$', views.article, name='article'),
]

# View (in blog/views.py)

def article(request, id):
    # 展示某篇文章

 URL naming and reverse() method

Did you notice? We also gave the URL a name 'article' in the above code. This name is very useful, it is equivalent to giving the URL the name of a global variable. It allows you to explicitly reference it anywhere in Django, especially in templates. Suppose you need to link to a specific article in the template, which is better?

方法1: 使用命名URL
<a href="{% url 'article' id %}">Article</a>

方法2: 使用常规URL - 不建议
<a href="blog/article/id">Article</a>

If you haven't realized the benefits of method 1, think about it, suppose you need to change all template links from blog/article/id to blog/articles/id, which method is faster? Change all templates, or change a letter in the URL configuration?

Unfortunately, named URLs are generally only used in templates, not directly in views. If we have a named URL, how do we convert it to a regular URL to use in the view? This is easy to do with the reverse() method provided by Django. Assuming that different apps (such as news and blog) have article named URLs, how can we distinguish them? We just need to add the blog namespace in front of article.

from django.urls import reverse

# output blog/article/id
reverse('blog:article', args=[id])

How URLs point to class-based views (View)

Currently path and re_path can only point to a function or method in the view view, but not to a class based view.

Django provides an additional as_view() method that can disguise a class as a method.

This is very important when you use Django's built-in view classes or custom classes. The specific usage is as follows:

# blog/urls.py
from django.urls import path, re_path

from . import views

urlpatterns = [
      path('', views.ArticleList.as_view(), name='article_list'),
    path('blog/article/<int:id>/', views.article, name = 'article'),
    re_path(r'^blog/article/(?P<id>\d+)/$', views.article, name='article'),
]

# View (in blog/views.py)
from django.views.generic import ListView
from .views import Article

class ArticleList(ListView):

    queryset = Article.objects.filter(date__lte=timezone.now()).order_by('date')[:5]
    context_object_name = 'latest_article_list'
   template_name = 'blog/article_list.html'

def article(request, id):
    # 展示某篇文章

Pass extra parameters via URL method

When you configure the URL, you can also pass additional parameters to the view in the form of a dictionary instead of writing this parameter in the link. As shown in the following case:

# blog/urls.py
from django.urls import path, re_path

from . import views

urlpatterns = [

    path('', views.ArticleList.as_view(), name='article_list', {'blog_id': 3}),
    re_path(r'^blog/article/(?P<id>\d+)/$', views.article, name='article'),
]

 

Guess you like

Origin blog.csdn.net/qq_52385631/article/details/126951386