简体   繁体   English

Django 2.0.7 URL app_name的工作方式

[英]Django 2.0.7 how URL app_name works

I'm working on a project built with python 3.4.3 and django 2.0.7 and I'm stuck with URLs files logic. 我正在开发一个使用python 3.4.3和django 2.0.7构建的项目,但我一直坚持使用URLs文件逻辑。 I still haven't figured this error out: "ImportError: No module named 'fields'. 我仍然没有发现此错误:“ ImportError:没有名为'fields'的模块。

Here's what I have: 这是我所拥有的:

urls.py urls.py

from django.urls import include, path
from django.contrib import admin
from bridge.core import views as core_views

urlpatterns = [
    path('', core_views.home),
    path('backoffice/fields/', include('fields.urls', namespace='backoffice')),
    path('admin/', admin.site.urls),
]

fields/urls.py 田/ urls.py

from django.urls import path
from . import views

app_name = 'fields'

urlpatterns = [
    path('', views.list_fields, name='list_fields'),
]

What does "app_name" is supposed to be filled with? “ app_name”应该填写什么? How "app_name" works? “ app_name”如何工作?

According to there files, when I submit "/backoffice/fields/" in the browser django should invoke "views.list_fields", shouldn't? 根据那里的文件,当我在浏览器中提交“ / backoffice / fields /”时,django应该调用“ views.list_fields”,不是吗?

If any other information is required to help me with this issue, just let me know and I'll provide it as quickly as possible. 如果需要其他任何信息来帮助我解决此问题,请告诉我,我会尽快提供。

Thanks in advance 提前致谢

The app_name is used to reference your urls else where. app_name用于在其他位置引用您的urls You will also see referenced as namespacing . 您还将看到引用为namespacing If you would have two different apps with the same url name , it wouldn't always pick the right one. 如果您有两个具有相同url name不同应用程序,那么它并不总是会选择正确的应用程序。 So you namespace them and call them as such: 因此,您为它们命名空间并按如下方式调用它们:

{% url 'employee:name' %}
{% url 'customer:name' %}

You can use the names in more than just template tags, like reverse('employee:name') and such. 您不仅可以在模板标签中使用名称,还可以使用reverse('employee:name')等。

The problem you are running into is that it doesn't know where to find your urls file. 您遇到的问题是它不知道在哪里可以找到您的urls文件。 If your installed app is bridge.fields , try using include(bridge.fields.urls) . 如果您安装的应用程序是bridge.fields ,请尝试使用include(bridge.fields.urls)

To fully find your problem I would have to see you project folder structure as to where your main urls file is and where your fields.urls is. 为了完全找到您的问题,我将不得不看到您的project文件夹结构,其中包括您的主urls文件所在的fields.urls以及fields.urls所在的fields.urls Remember that fields.urls really means a file named urls.py that is located in the folder named fields . 请记住, fields.urls实际上意味着位于名为fields的文件夹中的名为urls.py的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM