Django 中的循环导入
Circular Import in Django
我一直在尝试 运行 使用 Django 的客户端-服务器应用程序。当我尝试 运行 我在 Django 中的服务器时,出现以下错误。
django.core.exceptions.ImproperlyConfigured: The included URLconf '' 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.
项目urls.py-
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('chat.views')),
]
应用的 views.py -
from django.shortcuts import render
from django.http import JsonResponse
def home(request):
if request.method == 'POST':
if request.is_ajax():
//code
return JsonResponse(data)
return render(request,'index.html')
我哪里错了?
include
方法采用应用程序 urls.py 模型而不是 views.py。您需要在应用程序中创建 urls.py
文件,并在项目 url 文件中将 url(r'^', include('chat.views'))
替换为 url(r'^', include('chat.urls'))
。见 django docs.
url.py 文件中的包含方法用于包含在其他文件中指定的 url 模式。当你这样做 url(r'^', include('chat.views')) 时,它无法在你的文件中找到 url 模式视图文件。因此给出错误:
django.core.exceptions.ImproperlyConfigured: The included URLconf ''
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.
我们通常会在我们的应用程序文件夹中创建一个 urls.py 文件,并在该文件中写入与该应用程序相关的所有 url 模式。
在你的应用程序文件夹中创建一个新的 urls.py 文件并在该文件中写入 url 模式。
然后将您的 应用程序的 urls.py 文件包含在 main urls.py 文件中,如下所示:-
url(r'^', include('chat.urls')),
您应用的 urls.py 文件应如下所示:
from django.conf.urls import url
urlpatterns = [
url(r'', views.home, name = "home")),
]
您可以从文档中找到更多关于 django urls 的信息:- django urls
如果您不想在您的应用程序目录中创建新的 urls.py 文件,那么您可以在 main urls.py 文件并在该文件中写入 url 模式。那么您的 main urls.py 文件将如下所示:-
from django.conf.urls import url,include
from django.contrib import admin
from chat.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', home, name = "home"),
]
我一直在尝试 运行 使用 Django 的客户端-服务器应用程序。当我尝试 运行 我在 Django 中的服务器时,出现以下错误。
django.core.exceptions.ImproperlyConfigured: The included URLconf '' 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.
项目urls.py-
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('chat.views')),
]
应用的 views.py -
from django.shortcuts import render
from django.http import JsonResponse
def home(request):
if request.method == 'POST':
if request.is_ajax():
//code
return JsonResponse(data)
return render(request,'index.html')
我哪里错了?
include
方法采用应用程序 urls.py 模型而不是 views.py。您需要在应用程序中创建 urls.py
文件,并在项目 url 文件中将 url(r'^', include('chat.views'))
替换为 url(r'^', include('chat.urls'))
。见 django docs.
url.py 文件中的包含方法用于包含在其他文件中指定的 url 模式。当你这样做 url(r'^', include('chat.views')) 时,它无法在你的文件中找到 url 模式视图文件。因此给出错误:
django.core.exceptions.ImproperlyConfigured: The included URLconf '' 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.
我们通常会在我们的应用程序文件夹中创建一个 urls.py 文件,并在该文件中写入与该应用程序相关的所有 url 模式。 在你的应用程序文件夹中创建一个新的 urls.py 文件并在该文件中写入 url 模式。
然后将您的 应用程序的 urls.py 文件包含在 main urls.py 文件中,如下所示:-
url(r'^', include('chat.urls')),
您应用的 urls.py 文件应如下所示:
from django.conf.urls import url
urlpatterns = [
url(r'', views.home, name = "home")),
]
您可以从文档中找到更多关于 django urls 的信息:- django urls
如果您不想在您的应用程序目录中创建新的 urls.py 文件,那么您可以在 main urls.py 文件并在该文件中写入 url 模式。那么您的 main urls.py 文件将如下所示:-
from django.conf.urls import url,include
from django.contrib import admin
from chat.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', home, name = "home"),
]