Django - Import views from separate apps

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

提交回复
热议问题