URLPatterns Understanding


Hello Everyone, I just joined the forum.

My doubt is that if I don’t create urls.py inside my app and just define my urlpatterns inside main project urls file. This way everything is working fine.

Then, what is the role of urls.py inside the app ?

Welcome @tyagiabhinav !

Yes, it is possible to put all your urls in your root urls.py file. (I’ve done it with a couple of small projects that don’t have more than 4 or 5 urls total.) However, it’s not something I generally recommend for active or growing projects.

By placing the urls in the urls.py file within an app, you’re making those urls part of that app. If you wish to package your app to reuse it in a different project, then those urls become part of the package, and are distributed with the app. (See Advanced tutorial: How to write reusable apps | Django documentation | Django)

Also, you (almost always) want the name parameter to be unique. Creating namespaces for your urls helps avoid naming conflicts between apps. Placing the urls in the app facilitates that. (See Writing your first Django app, part 3 | Django documentation | Django)

Thanks a lot @KenWhitesell.