Django urls.py无法识别URL

时间:2016-06-21 03:55:21

标签: django urlconf

我有一个具有以下文件结构的库应用程序

--|
  |
  |-- members--|
               |--views.py
               |--subviews--|
                            |
                            |--member_subviews.py
  |-- loans----|
               |--views.py
               |--subviews--|
                            |
                            |--loan_subviews.py
  |-- library--|
               |
               |--urls.py

loan_subviews.py中,有一种视图方法:

def generate_loan_view(request, member, loan):
    #Method body

然而,在我urls.py执行以下操作时,

url(r'^library/generate_invoice/(?P<pk>(\d+))/(?P<loan>(\d+))/'
    , members.subviews.member_subviews.generate_loan_view
    , name='library_generate_invoice'),   

它会引发NameError: name 'generate_loan_view' is not defined错误。如何修复以便URLConf文件可以看到我的视图方法?

3 个答案:

答案 0 :(得分:0)

第一行文件应为:

from . import views

然后替换行

members.subviews.member_subviews.generate_loan_view

views.generate_loan_view

答案 1 :(得分:0)

我将假设您没有导入任何观看次数,因为您还没有证明在您的urls.py示例中,您可以导入所有观看文件并像这样使用它们:

from library.members import views as members_views
from library.members.subviews import member_subviews as member_subviews
from library.loans import views as loans_views
from library.loans.subviews import loan_subviews as loan_subviews

patterns = [
    url(r'^library/generate_invoice/(?P<pk>(\d+))/(?P<loan>(\d+))/',
        members_subviews.generate_loan_view,
        name='library_generate_invoice'),
]

答案 2 :(得分:0)

确保每个文件夹都有 __ init __。py 文件。在这种情况下是子视图。