Why Django doesn't provide a urls.py file for apps?

Viewed 1297

When we start a new project, we are provided with a urls.py file. However, when we create apps within our project, Django doesn't provide the urls.py file for us and we are expected to create it manually. What is the exact logic/reason behind that? Are there even apps that don't necessarily have to have urls.py file?

1 Answers

First you have your project urls.py. This file is the starting point for django to look up if a certain url path exists.

When you use more than one app you normally tell django to look up their urls.py files after the project urls.py. So in this case creating a app-related urls.py makes sense in order to overview the entire url structure of your project divided into your apps.

When you use only one app it is actually not neccessary to have an app-related urls.py file because you dont have to bother about the entire url structure. You only use one app so the url is related to that app.

Normally you create a urls.py file even if you only have one app. This can have multiple reasons. But django lets you make this choice. First you dont know if you will add more apps in the future so you better already create a tidied up file structure from the beginning. Second because of the model-view-controller (django actually has a model-view-template structure) built-up you want to keep your views, models and paths together. It is much more cleaner and will help you in case your project grows up.

Hope it gets a little clearer and helps you a little bit.

Related