What is the recommended django project structure?

北慕城南 提交于 2019-12-05 00:51:30

问题


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.



来源:https://stackoverflow.com/questions/1629515/what-is-the-recommended-django-project-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!