Django 2.0 url()到path() [英] Django 2.0 url() to path()

查看:61
本文介绍了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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆