gpt4 book ai didi

python - 我的 "urls.py"文件在我的 Django 项目中放在哪里?

转载 作者:行者123 更新时间:2023-11-28 21:33:28 27 4
gpt4 key购买 nike

我正在使用 Django 和 Python 3.7。我用了PyCharm创建一个项目,它创建了以下目录结构(一些自定义添加是我自己的):

mainpage
__init__.py
__pycache__
admin.py
apps.py
fixtures
management
migrations
models.py
services.py
templates
mainpage
trending.html
tests.py
urls.py
views.py
mainpage_project
__init__.py
__pycache__
settings
wsgi.py
manage.py
templates
venv

文件“urls.py”如下所示

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

from . import views

urlpatterns = [
path('trending', trending, name='trending'),
]

我对“urls.py”应该去哪里以及它应该包含什么感到困惑。当我启动服务器时,出现以下错误

ModuleNotFoundError: No module named 'mainpage_project.urls'

(venv) localhost:mainpage_project davea$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103096950>
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mainpage_project.urls'

我的“urls.py”文件应该放在哪里,它应该包含什么?

最佳答案

你错过了urls.py在你的项目中mainpage_project .即,

mainpage_project
__init__.py
__pycache__
settings
wsgi.py
urls.py # Here

Vitor Freitas, A Complete Beginner's Guide to Django - Part 1的话来说:

<b>urls.py</b> is responsible for mapping the routes and paths in your project. For example, if you want to show something in the URL /trending/, you have to map it here first.

从你的目录结构mainpage_project是你的项目,mainpage是您的应用。

每个应用程序都可以有自己的 urls.py . Django 实际上会在您的项目文件夹中检查它。

典型 urls.py看起来像:

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

from mainpage import views

urlpatterns = [
path('trending', trending, name='trending'),
]

根据评论,您在添加 urls.py 时遇到导入错误因为在你的urls.py里面您正在尝试导入 views来自 mainpage_project的文件夹。

from . import views

.符号指的是调用它的当前目录,即 mainpage_project目录。

并且没有任何views.pymainpage_project 目录中。但是views.py存在于 mainpage 中目录。所以更新为:

from mainpage import views

关于python - 我的 "urls.py"文件在我的 Django 项目中放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54639590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com