【问题标题】:Trying to find the ImproperlyConfigured error in Django试图在 Django 中找到 ImproperlyConfigured 错误
【发布时间】:2020-12-20 17:34:58
【问题描述】:

我知道这个错误已经被问了很多,但是我阅读了很多帖子,但我找不到我的问题的解决方案。谁能帮我找出我的错误在哪里?

一直出现如下错误:

django.core.exceptions.ImproperlyConfigured: The included URLconf 'outside_world.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

outside_world\urls.py(项目的网址):

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

urlpatterns = [
    path(r'admin/', admin.site.urls),
    path(r'', views.detail, name='detail'),
    path(r'trip/', include("trip.urls")),
]

行程\urls.py:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

trip\views.py:

from django.shortcuts import render
from django.http import HttpResponse

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

我的项目树: Project Tree

当我从 outside_world\urls.py 中删除 path(r'trip/', include("trip.urls")), 时,它工作得很好,但我需要维护这一行。

【问题讨论】:

  • path(r'trip/', include("trip.urls")),

标签: python django django-views django-urls django-errors


【解决方案1】:

将这两个 url 移动到项目的 urls.py 并从 outside_world\urls.py 中移出

path(r'admin/', admin.site.urls)
path(r'trip/', include("trip.urls"))

此外,trip\views.py 中有一个未定义的索引定义/类。如果你去 http(s)://YourIP/YourProjectName/trip 会导致错误。你应该定义 index. 如果您正在键入以下内容之一,它将正常工作,因为您已经定义了这些视图。 Number 是您定义的整数 (QuestionID)

  • http(s)://YourIP/YourProjectName/trip/Number 或
  • http(s)://YourIP/YourProjectName/trip/Number/results 或
  • http(s)://YourIP/YourProjectName/trip/Number/Vote 因为它们是 已定义。

【讨论】:

  • 谢谢,但项目名为 outside_world,所以 path(r'admin/', admin.site.urls) path(r'trip/', include("trip.urls")) 已经在项目的 url 中。我已经修复了您指出的第二件事,并且错误仍然存​​在。
  • 项目里怎么会有意见?有什么不对吗?你会编辑帖子并粘贴你的项目树吗?
  • 我收到了这个错误,所以我在我的项目文件夹中添加了一个 views.py 文件,看看我是否至少可以直接从项目文件夹中打开一个 url。我在上面添加了我的项目树,以便您可以看到它。
  • 我猜你可能不得不删除这两行。 "from .import views" 和 "path(r'', views.detail, name='detail')," ..我没有看到任何其他问题..但这是我的猜测
  • 感谢您尝试帮助我,但我删除了这些行,但仍然无法正常工作。如果没有其他人回答这个帖子,我将从头开始这个项目。
猜你喜欢
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2013-01-25
  • 2016-10-29
  • 2020-12-09
相关资源
最近更新 更多