Django - Import views from separate apps

后端 未结 5 861
轻奢々
轻奢々 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:42

    The URLconfs documentation gives an example of the same situation

    You can skip the imports and separate the urls by app as such:

    urlpatterns = patterns('books.views',
        (r'^/book/search/$', 'search'), #calls books.views.search
    )
    
    urlpatterns += patterns('contact.views', #make note of the '+='
        (r'^/contact/search/$', 'search'), #calls contact.views.search
    )
    
    0 讨论(0)
  • 2021-02-19 08:45

    The reason I’m answering this question is because it was answered years ago and those answers are not correct or useful anymore for newer Django versions, or there is a better practice you should know about.

    So, if you have more than one app in your Django project then you should use a new urls.py file for every one of your apps. It means that if you start a new app then you have to manually create a new file called urls.py in the subfolder of your new app. Many beginners first do not understand why this is good, but this is a good practice if you plan creating more apps in one Django project.

    When you start a project, the urls.py file automatically created in your project folder, but if you create/start a new app in Django, then it is a good practice if you create a separate urls.py for that app in its own folder. (And that way you will never have the "importing different app's views into urls.py" problem in the first place).

    After you created the urls.py file for your app, then you have to include that app’s urls.py file in your project’s urls.py file in the following way:

    Let’s see an example when you create a new app called ‘my_new_app’. This is how your project’s main urls.py file should look like:

    from django.conf.urls import url, include
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^my_new_app/', include('my_new_app.urls')),
    ]
    

    In your project’s urls.py file you have to import the ‘include’ method, then you can include your my_new_app urls.py file in your project’s main urls.py file. In your my_new_app folder you have to manually create a urls.py file as I stated above. Then you have to use that file for all of your urlpatterns of your my_new_app. Then of course this way it’s going to be automatically included in your project’s main urls.py file.

    So this is then how your my_new_app own urls.py file should look like:

    from django.conf.urls import url
    from my_new_app import views
    
    urlpatterns = [
        url(r'^$', views.index, name = "index"),
    ]
    

    Assuming that you also created a first view called ‘index’ in your ‘my_new_app/views.py file.

    my_new_app/views.py file look like this:

    from django.shortcuts import render
    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("Hello World!")
    

    And you can check your my_new_app in your browser at:

    http://localhost:8000/my_new_app
    

    (Of course you can give any url to your my_new_app in your project's urls.py file.)

    Now, you can create another app, in your Django project, called my_second_app and you should repeat the above steps for that app too. This way you will not have any problem importing views from different apps into urls.py files. This would be a very basic “good practice solution” for this problem in 2017 in Django 1.11.

    0 讨论(0)
  • 2021-02-19 08:48

    Heres the approach i took for different view/API versions:

    from django.urls import path
    
    from my_app import views as api_v1
    from my_app import views_v2 as api_v2
    
    urlpatterns = [
        path('view_one', api_v1.ViewOne.as_view()),
        path('view_two', api_v2.ViewTwo.as_view()),
    ]
    
    0 讨论(0)
  • 2021-02-19 08:50
    from books import views
    from contact import views
    

    You are overwriting the name views. You need to import them as different names or as absolute names.

    import books.views
    import contact.views
    

    ... or ...

    from books import views as books_views
    from contact import views as contact_views
    

    Then use the correct name when defining your URLs. (books.views.search or books_views.search depending on the method you choose)

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题