如何从Django请求获取urlpattern

2024-04-25 06:13:17 发布

您现在位置:Python中文网/ 问答频道 /正文

例如:

from django.urls import include, path

urlpatterns = [
    path('index/', views.index, name='main-view'),
    path('bio/<username>/', views.bio, name='bio'),
    path('articles/<slug:title>/', views.article, name='article-detail'),
    path('articles/<slug:title>/<int:section>/', views.section, name='article-section'),
    path('weblog/', include('blog.urls')),
    ...
]

{1>不想从请求中得到一个值

我使用Django 1.8.2


Tags: pathdjangonamefromimportindexincludetitle
1条回答
网友
1楼 · 发布于 2024-04-25 06:13:17

如果只想要实数path,可以使用request.pathdoc

如果您想要<slug:title>参数,只需通过function arguments进入{}函数。在

def some_view(request, title):
    # do your job.

编辑

你需要这两个函数。在

^{pr2}$

必须在^{上设置name

urlpatterns = [
    ...
    path('articles/<slug:title>', your_view, name='your_view_name'),
    ...
]

最后你可以得到你的模式在你的看法。在

def your_view(request, title):
    view_name = get_view_name_by_path(request.path)
    pattern = find_url_pattern_by_name(view_name)
    ...

相关问题 更多 >