如何在Django应用程序中创建分层URL?

时间:2019-01-25 10:39:05

标签: django django-urls

我有多个基于Django的应用程序项目,并且由于模型数量的原因,某些应用程序的URL方案变得越来越复杂。因此,我正在寻找一种在应用程序内构建的分层网址结构的方法。

在我的项目的urls文件中,我执行以下操作。

Option Explicit

Sub DoItForAllWorkbooks()
    Dim myWorkbook As Workbook
    Dim myWbFullNames(100) As String
    Dim iCt As Integer

    ' You might to want to create such a list with Excel:
    '="'        myWbFullNames("&B3&") = ""C:\mySpecialFolder\myFile0"&B3&".xlsx"""

    myWbFullNames(1) = "C:\mySpecialFolder\myFile01.xls"
    myWbFullNames(2) = "C:\mySpecialFolder\myFile02.xls"
    myWbFullNames(3) = "C:\mySpecialFolder\myFile03.xls"
    myWbFullNames(4) = "C:\mySpecialFolder\TabColor4.xlsx"
    myWbFullNames(5) = "C:\mySpecialFolder\myFile05.xls"

    For iCt = 1 To 5
        Workbooks.Open myWbFullNames(iCt)
        Set myWorkbook = ActiveWorkbook
        Call Copy_Green_Sheets_NEW(ActiveWorkbook)
        myWorkbook.Close
    Next iCt
End Sub

Sub Copy_Green_Sheets_NEW(SourceWorkbook as workbook)
Dim wb As Workbook
Dim ws As Worksheet
    Set wb = Workbooks("Target workbook")
    'For Each ws In Workbooks("source workbook").Worksheets --- OLD
    For Each ws In SourceWorkbook.Worksheets
        If ws.Tab.Color = RGB(0, 255, 0) Then
            ws.Copy After:=wb.Sheets(wb.Sheets.Count)
        End If
    Next ws
End Sub

from order import urls as order_urls 应用中,我有orderurls.py目录,分别包含每个模型的单独网址格式,如下所示。

enter image description here 在应用程序的urls.py文件中,按如下所示导入模型的url。

urls

这给了我错误:from urls import rental as rental_urls urlpatterns = [ url(r'^rental-request/', include(rental_urls)), ] 如果我输入ModuleNotFoundError: No module named 'urls',则会给我循环导入错误。

我不确定这是否符合我的要求。任何人都可以解释实现它的正确方法吗?

1 个答案:

答案 0 :(得分:3)

在同一个文件夹中同时有一个名为urls的文件夹(带有一个__init__.py文件)和一个文件urls.py可能会导致无法从中加载模块order.urls项目中的任何地方。 Python如何知道必须加载哪个文件?

考虑此结构:

├── main.py
├── urls
│   └── __init__.py
└── urls.py

每个文件的内容:

# urls/__init__.py
urlpatterns = "I'm in folder"

# urls.py
urlpatterns = "I'm in file"

# main.py
import urls
print(urls.urlpatterns)

运行main.py时,结果为:

% python main.py
I'm in folder

可能的解决方案: 您可以删除urls.py并将其内容移动到urls/__init__.py,或重命名文件夹urls以避免冲突并相应地更新导入(在urls.py中)