Django - Import views from separate apps

后端 未结 5 835
轻奢々
轻奢々 2021-02-19 07:59

I\'m new to Django and working my way through \"The Django Book\" by Holovaty and Kaplan-Moss. I have a project called \"mysite\" that contains two applications called \"books\

5条回答
  •  清酒与你
    2021-02-19 08:53

    Disclaimer: Not a Django answer

    The problem is with these two lines:

    from books import views
    from contact import views
    

    The second import is shadowing the first one, so when you use views later you're only using the views from contact.

    One solution might be to just:

    import books
    import contact
    
    urlpatterns = patterns('',
    ...
    (r'^search/$', books.views.search),
    (r'^contact/$', contact.views.contact),
    ...
    

    I'm not sure, but I also think that you don't actually need to import anything and can just use strings in your pattern, something like: 'books.views.search'.


    Another possiblity is to follow Simon Visser suggestion:

    from books.views import search
    from contact.views import contact
    

提交回复
热议问题