如何解决有关“BlogProject.urls”的“django.core.exceptions.ImproperlyConfigured”错误?

问题描述 投票:0回答:1

我在 Django 项目中遇到“django.core.exceptions.ImproperlyConfigured”错误。当我使用 tail -f /var/log/cron.log 检查 cron.log 文件时,我看到以下错误消息:

File "/usr/local/lib/python3.12/site-packages/django/core/checks/urls.py", line 61, in _load_all_namespaces
    url_patterns = getattr(resolver, "url_patterns", [])
File "/usr/local/lib/python3.12/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.12/site-packages/django/urls/resolvers.py", line 748, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'BlogProject.urls' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

我的crontab内容如下:

*/1 * * * * /usr/local/bin/python /usr/src/BlogProject/manage.py flushexpiredtokens > /var/log/cron.log 2>&1.

我有一个 urls.py 文件,我在其中为 Django 项目定义 URL 模式。以下是文件内容:


from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from users.views import RegisterView

if settings.DEBUG:
    urlpatterns = [
        path("admin/", admin.site.urls),
        path("", include("users.urls")),
        path("", include("blogger.urls")),
        path("", include("administration.urls")),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



y settings.py 文件包含我的 Django 项目的配置设置。以下是文件内容:


from celery.schedules import crontab
import os
from pathlib import Path
from datetime import timedelta

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = ""

DEBUG = bool(os.environ.get("DEBUG", default=0))

ALLOWED_HOSTS = ['example.com', 'localhost', '127.0.0.1']

AUTH_USER_MODEL = "users.User"

SITE_ID = 1

WEBSITE_URL = 'http://localhost:8000'

SIMPLE_JWT = {
    # JWT configuration
}

# Other configurations...

INSTALLED_APPS = [
    # Installed apps...
    'django_crontab',  # Django Crontab
]

# Other configurations...

DATABASES = {
    'default': {
        # Database configuration...
    }
}

# Password validation and other configurations...

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True

STATIC_ROOT = BASE_DIR / "staticfiles"

STATIC_URL = "static/"

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
python docker django-rest-framework cron-task django-crontab
1个回答
0
投票

在 Django 的 URL 配置中,路径

""
匹配空字符串,这意味着它匹配每个 URL。当您使用
""
包含多个 URL 模式时,它们将全部匹配每个 URL,从而导致潜在的冲突或意外行为。

要解决此问题,您应该为每个包含的 URL 配置指定唯一的路径前缀。这有助于区分不同的 URL 模式集。例如:

urlpatterns = [
    path("admin/", admin.site.urls),
    path("users/", include("users.urls")),
    path("blogger/", include("blogger.urls")),
    path("administration/", include("administration.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

每个包含的 URL 配置(

users.urls
blogger.urls
administration.urls
)都以唯一路径为前缀(
"users/"
"blogger/"
"administration/"
)。这可确保每个应用程序的 URL 模式范围正确,并且不会相互冲突。

© www.soinside.com 2019 - 2024. All rights reserved.