What is the recommended django project structure?

2019-03-30 21:48发布

问题:

I haven't exactly found the answer to this, maybe there is no best one. Django docs are not clear on this thing.

I'm not sure what is a good practice to set up a django project. Sure I have re-usable apps. But I always need some glue-code.

I find myself always creating "core" app for each project, which usually serves homepape.

Then in project url config I do something like this:

(r'^/$', include(core.urls))

Is this the way to go? Or do you have a better idea?

回答1:

I think it's a good idea to use a glue app/module that also contains further helper functions/reusable code (if any), however I'm not sure if that's the way the other djangonauts do these kind of stuff.

Also, in order to match the homepage I think that the correct regexp is r'^$'. With the above solution you propose, you'll have to be careful because every url defined in core.urls will be 'mounted' under your site's root directory.

Also, and for the case of the homepage I used something like this

(r'^$', 'apps.core.views.homepage')

just to distinguish this url. I guess it's just a matter of how one wants things organized.



回答2:

Don't put the slash and the dollar. This is how I did it.

(r'^', include('core.urls')),

You're already on the right track. ;)



回答3:

The approach you mention is a good one. I tend to stash away stuff like that into a views.py file in the root of the project. There is already a urls.py in the root folder.