为什么这个python代码会崩溃我的网站?而这个其他的python代码不?

时间:2018-05-27 18:24:40

标签: python django python-3.x

为什么下面的第一个代码会导致我的网站崩溃,而第二个代码却没有?

以下是崩溃网站的代码:

from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls'))
]

这是追溯:

Traceback (most recent call last):
File "learning_log/learning_log/urls.py", line 17, in <module>  from django.contrib import admin
ModuleNotFoundError: No module named 'django'

以下是不会崩溃的代码:

from django.urls import path
from django.contrib import admin

urlpatterns = [
   path('admin/', admin.site.urls),
]

我使用的是python 3和django 2.

1 个答案:

答案 0 :(得分:0)

假设包含学习位置的格式为mysite / posts / the_restof_learning_urls,则必须执行url(r'^posts/', include('learning_logs.urls', namespace='posts')),

所以你有(path('',)这是一个空字符串,并且必须添加学习的网址。

之后,您必须自定义帖子/ learning.urls ---&gt;

    from posts.views import 
(posts_list,posts_create,posts_delete,posts_detail,posts_update)
from posts import views as posts_views

app_name= 'posts'    
    urlpatterns = [
        url(r'^$',posts_views.posts_list, name='list'),
        url(r'^create/$', posts_views.posts_create),
        url(r'^(?P<slug>[\w-]+)/$', posts_views.posts_detail, name='detail'),
        # url(r'^(?P<slug>[\w-]+)/$', PostDetailView.as_view(), name='detail'), #Django Code Review #3 on joincfe.com/youtube/
        url(r'^(?P<slug>[\w-]+)/edit/$', posts_views.posts_update, name='update'),
        url(r'^(?P<slug>[\w-]+)/delete/$', posts_views.posts_delete),
        #url(r'^posts/$', "<appname>.views.<function_name>"),

]

您必须根据自己的代码自定义它,方法是使用您的学习应用更改帖子