【问题标题】:Django possible circular importDjango 可能的循环导入
【发布时间】:2021-10-15 05:35:17
【问题描述】:

我正在建立一个新的“游戏”网站。我已经启动了一个新的应用程序 Games,使用代码(如下)设置 games/models.py。在 makemigrations 游戏和迁移之后,我能够登录到管理员并添加条目。

然后我开始创建 URL、视图和一个模板。创建它们后,当我运行服务器时,出现以下错误:

url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e django.core.exceptions.ImproperlyConfigured: 包含的 URLconf '' 没有 似乎有任何模式。如果您在 文件,那么问题可能是由循环导入引起的。

我注意到的第一件事是games/models.py(使用PyCharm)的第一行(from django.conf import settings)是灰色的。我不知道这是否与错误有关:

谁能发现我哪里出错了?这是相关代码。

# games/models.py
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse


class Game(models.Model):
    title = models.CharField(max_length=255)
    date_create = models.DateTimeField(auto_now_add=True)
    date_start = models.DateTimeField(auto_now_add=False)
    body = models.TextField()
    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )
    answer_one = models.CharField(max_length=1)
    answer_two = models.CharField(max_length=1)
    answer_three = models.CharField(max_length=1)
    answer_four = models.CharField(max_length=1)
    answer_five = models.CharField(max_length=1)
    answer_six = models.CharField(max_length=1)
    answer_seven = models.CharField(max_length=1)
    payout_total = models.FloatField(null=True, blank=True, default=1.0)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('game_detail', args=[str(self.id)])

游戏/管理员

   # games/admin.py
    from django.contrib import admin
    from .models import Game
    
    admin.site.register(Game)

配置/网址

# config/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('games/', include('games.urls')),
    path('', include('pages.urls')),
]

游戏/网址

# games/urls.py
from django.urls import path
from .views import GameListView

urlpattrns = [
    path('', GameListView.as_view(), name='game_list'),
]

游戏/views.py

# games/views.py
from django.views.generic import ListView
from .models import Game

class GameListView(ListView):
    template_name = 'game_list.html'

【问题讨论】:

  • 不确定是否只是复制/粘贴错误,但游戏/urls.py 中的 urlpatterns 拼写错误。这可能会导致此错误。

标签: python django exception django-models django-views


【解决方案1】:

正如错误所述,当 django 在您的 urlconf 中找不到有效的 urlpatterns 时会发生这种情况。这可能有很多原因,但在你的情况下,我最有可能的一个原因是 urlpatterns 在 games/urls.py 中被拼写为“urlpattrns”。尝试修正错字然后再次运行。

【讨论】:

  • 哦,天哪,泰穆尔·易卜拉欣。谢谢你看到那个。我想工作太累了。
猜你喜欢
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多