[英]How to upgrade the code to Django 2 or use something similar to “path” in Django 1.9?
I was using Django 1.9 and upgraded to Django 2.0 as follows: 我正在使用Django 1.9,并按如下所示升级到Django 2.0:
pip install -U django
Check: 校验:
python3 -m django --version
> 2.1.7
The main reason to update django was the usage of path
. 更新django的主要原因是使用path
。 As far as I understand, path
is unavailable in Django 1.9. 据我了解, path
在Django 1.9中不可用。
So, I have this import in my code: 所以,我的代码中有这个导入:
from django.conf.urls import url
from django.urls import path # !!! this line fails !!!
from django.contrib import admin
from my_tests import views
urlpatterns = [
path('admin/', admin.site.urls),
path("test", views.test_endpoint, name='test_endpoint')
]
However when I run the code, it throws the following error: 但是,当我运行代码时,它将引发以下错误:
ModuleNotFoundError: No module named 'django.urls'
It looks like it is still using an old version of django. 看起来它仍在使用旧版本的django。
How to solve this error? 如何解决这个错误? How should I upgrade to Django 2 or how can I use something similar to path
in Django 1.9? 我应该如何升级到Django 2或如何使用与Django 1.9中的path
类似的东西?
This is how it was done before Django 2: 这是在Django 2之前完成的操作:
from django.conf.urls import include, url
urlpatterns = [
url(r'^index/$', index_view, name='main-view'),
url(r'^test/', test_view, name='test_endpoint'),
url(r'^weblog/', include('blog.urls')),
...
]
Taken from Documentation 摘自文档
There was no path module thus it throws an error. 没有路径模块,因此会引发错误。
Besides I recommend updating to Django 2.1 if it is not to much effort. 此外,如果不花太多力气,我建议您更新到Django 2.1 。
Here is a guide on how to do it. 这是有关操作方法的指南 。 For version 2.1 you have to use one of the following python versions: 3.5, 3.6, 3.7. 对于2.1版,您必须使用以下python版本之一:3.5、3.6、3.7。 Django-Python Django的Python的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.