如何从Django主URL文件中的应用程序导入URL [英] How to import urls from apps in django main urls file
问题描述
我有以下项目结构.
这是我的INSTALLED_APPS数组.
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',"climanmanage.clients"]
然后我尝试将 clients.urls
文件添加到父 urls
文件中,如下所示.
导入管理员从django.urls导入包括路径urlpatterns = [路径("admin/",admin.site.urls),路径('clients/',include('clients.urls'))]
但是我遇到了这个错误. ModuleNotFoundError:没有名为客户端"的模块
我可以使用以下路径解决此问题. include('climemanage.clients.urls')
,但我想从路径中跳过 climemanage
.
我尝试了不同的方式,
导入客户端导入climemanage.clients来自climemanage进口客户
但是没有任何作用.
您的项目结构错误.似乎 startproject
部分或 startapp
部分可能出了问题.如果您可以列出到目前为止的工作方式,那将是最好的选择.请查看我在回答末尾添加的结构.
在您的情况下,您的主要 settings.py
位于 clients
下.这意味着 clients
是 startproject
的结果.目前,您没有 app
,但是有一堆文件应该随 app
一起出现,现在位于 migrations
文件夹下.>
因此,您将永远无法添加从 clients.urls
到主 urls.py
的任何内容.因为您的 clients
是您的主要 urls.py
所在的位置.
我在这里放了我通常要做的事情(此Mac版本,如果您使用的是Windows,则需要查看django教程)
$ mkdir clienmanage$ cd clienmanage$ python3 -m venv myvenv#设置虚拟环境$ source myvenv/bin/activate#进入虚拟环境完成此步骤后,您可以安装django
然后,当您运行 startproject
命令时,请确保按以下方式运行:
django-admin startproject项目.
注意最后的.
.
在那之后,您应该具有以下结构:
climanmanage| --- manage.py| ---项目|| --- __init__.py|| --- settings.py|| --- urls.py|| --- wsgi.py| ---虚拟|| ___ requirements.txt
那么您将:
$ python manage.py startapp客户端
climanmanage|| ---客户#这是应用程序|| ---移民|| --- models.py|| --- views.py|| --- apps.py| --- manage.py| ---项目|| --- __init__.py|| --- settings.py|| --- urls.py|| --- wsgi.py| ---虚拟#一个文件夹|| ___ requirements.txt
此步骤之后,django将不会自动生成 app
级别的 urls.py
和 forms.py
.您应该在 app
文件夹中生成这些文件(在我的示例中为 clients
)
**** 如果要将应用程序放置在其他目录中您可以指定目的地.
$ python manage.py startapp< app_label>[目的地]
在上述情况下,如果要将 app
放入 project
$ python manage.py startapp客户端项目/客户端
如果您采用这种方式,请确保在 INSTALLED_APPS
中添加 project.clients
而不是 clients
.
I have the following project structure.
This is my INSTALLED_APPS array.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'climemanage.clients'
]
Then I tried to add the clients.urls
file in the parent urls
file as the below.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('clients/', include('clients.urls'))
]
But I'm getting this error. ModuleNotFoundError: No module named 'clients'
I can solve this by using the following path. include('climemanage.clients.urls')
but I want to skip climemanage
from the path.
I have tried different ways as,
import clients
import climemanage.clients
from climemanage import clients
But nothing is working.
Your project structure is wrong. It seems either startproject
part or startapp
part might gone wrong. It might be best if you could list out how you got up to this point. Please see the structure I put at the end of my answer.
In your case, your main settings.py
is under clients
. This means that clients
is the result of startproject
. Currently you do not have an app
, but you have a bunch of files that should be appeared with app
that now under migrations
folder.
Beause of that, you will never be able to add anything from clients.urls
to main urls.py
. Because your clients
is the place where your main urls.py
located.
I put what I normally do here (this mac version, if you are using windows, you need check django tutorial)
$ mkdir clienmanage
$ cd clienmanage
$ python3 -m venv myvenv # set up virtual environment
$ source myvenv/bin/activate # go into virtual envionment
After this step you can install django
then after that, when you run your startproject
command, make sure run like below:
django-admin startproject project .
Note the .
at the end.
After that, you should have structure like:
clienmanage
|--- manage.py
|--- project
| |--- __init__.py
| |--- settings.py
| |--- urls.py
| |--- wsgi.py
|--- virtual
|
|___ requirements.txt
then you will:
$ python manage.py startapp clients
clienmanage
|
|---clients # this is the app
| |---migrations
| |---models.py
| |---views.py
| |---apps.py
|--- manage.py
|--- project
| |--- __init__.py
| |--- settings.py
| |--- urls.py
| |--- wsgi.py
|--- virtual # a folder
|
|___ requirements.txt
After this step, django will not automatically generate app
level urls.py
and forms.py
. You should generate these files inside the app
folder (in my example case, clients
)
****If you want to put app in a different directory You can specify destination.
$ python manage.py startapp <app_label> [destination]
in the above case, if you want to have your app
inside of project
$ python manage.py startapp clients project/clients
If you took this route, make sure to add project.clients
rather than clients
in your INSTALLED_APPS
.
这篇关于如何从Django主URL文件中的应用程序导入URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!