Django 2.0 url()到path()

编程入门 行业动态 更新时间:2024-10-24 04:44:18
本文介绍了Django 2.0 url()到path()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在学习Django。 到目前为止,我使用Django 1.1,但现在使用Django 2.0。 Django 2.0使用path()而不是url(),但我对此并不太清楚。

I am currently learning Django. Until now I was working with Django 1.1 but now I am working with Django 2.0. Django 2.0 uses path() instead of url() and I don't quiet understand that.

在Django 1.1中,我的网址看起来像这样:

In Django 1.1 my urls looked like this:

url(r'^about/$', views.AboutView.as_view(), name='about'),

现在使用Django 2看起来像这样

Now with Django 2 it looks like this

path('about/', views.AboutView.as_view(), name='about'),

到目前为止还不错,但我只是不了解如何进行转换

So far so good but I just don't undertand how I can convert this

url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),

以便与新版本一起使用。仅将url替换为path无效,将url更改为re_path也不可行。有人可以帮我解决这个问题吗?

So that it works with the new version. Just chagning url to path doesn't work, and changing url to re_path doesn't work either. Can someone help me with that Problem?

预先感谢

推荐答案

要放置正则表达式

path('post/<int:pk>', views.PostDetailView.as_view(), name='post_detail'),

我刚刚尝试并使用您拥有的相同网址对此进行了测试,我的项目之一,它有效。通过在此处使用关键字 int ,它们使url更简单易读。

I just tried and tested this with the same url that you have, in one of my projects and it works. They have made the url more simpler and readable by letting to use the keyword int there.

这是执行此操作的新方法,请阅读发行说明 他们清楚地提到了这些更改。

This is the new method to do it, Please read the release notes they have clearly mentioned these changes.

更多推荐

Django 2.0 url()到path()

本文发布于:2023-05-31 10:06:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   path   url

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!