简体   繁体   English

简单的 Django 项目结构

[英]Simple Django Project Structure

I have built the virtual environment and root directory for my django project, which will be a simple website for a event venue.我已经为我的 django 项目构建了虚拟环境和根目录,这将是一个用于活动场地的简单网站。 The site will simply have a few different tabs on the navigation bar which shows the user some of the venue specifics which include pricing, a photo gallery, and the venue's history.该网站将在导航栏上有几个不同的选项卡,向用户显示一些场地细节,包括定价、照片库和场地历史。

My problem is that there is so much conflicting information on the web concerning "The best practice for project structure".我的问题是 web 关于“项目结构的最佳实践”的信息有很多相互矛盾的信息。

Should I create an app for the home page and each of the pages that follow, or just create a core app that houses the majority of the project?我应该为主页和随后的每个页面创建一个应用程序,还是只创建一个包含项目大部分内容的核心应用程序? If this is the case, is this project a good example?如果是这样,这个项目是一个很好的例子吗? --> https://github.com/justdjango/django-ecommerce --> https://github.com/justdjango/django-ecommerce

Does anyone know of a simple project that I can reference?有谁知道我可以参考的简单项目?

Again this is a pretty simple project with only a few urls and no forms.同样,这是一个非常简单的项目,只有几个 url,没有 forms。

I would greatly appreciate anyone who has taken the time to read through this and help me.我将非常感谢任何花时间阅读并帮助我的人。

from my experience using Django, there is no definite answer for that it all depends on you and what you are comfortable with, I hope this helps.根据我使用 Django 的经验,没有明确的答案,这完全取决于您以及您对什么感到满意,我希望这会有所帮助。

Templates/static files模板/静态文件

I put these templates and static files into global templates/static directory, not inside each app, If you are full-stack developer working alone or in a small team, you can create per-app templates/static directory.我将这些模板和 static 文件放在全局模板/静态目录中,而不是在每个应用程序中,如果您是单独或在一个小团队中工作的全栈开发人员,您可以创建每个应用程序的模板/静态目录。 It's really just a matter of taste.这真的只是口味问题。

[projectname]/
├── [projectname]/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
|
|── [App1]
|── [static]/ 
|   ├── [css]
|   ├── [Javascript]
|   └── [Images]
|
|── [templates]
|── manage.py
└── requirements.txt

in my opinion you just need one app let's call it web since it is used for website only.在我看来,您只需要一个应用程序,我们称它为web ,因为它仅用于网站。 The steps I follow in creation of a simple project after creating repository and database and make it running is:在创建存储库和数据库并使其运行之后,我在创建一个简单项目时遵循的步骤是:

  1. Create static, media and templates folders创建 static、媒体和模板文件夹
  2. python manage.py startapp web
  3. Add web in INSTALLED_APPS in settings.py file在 settings.py 文件的INSTALLED_APPS中添加 web
  4. Put html files in templates folder and other folders(js,css,images,fonts..) in static folder将 html 文件放入 templates 文件夹和其他文件夹(js,css,images,fonts..)在static文件夹中
  5. Set URL to the new app in project_name > urls.py在 project_name > urls.py 中将 URL 设置为新应用
  6. Create view for index page in web.viewsweb.views中为索引页面创建视图
  7. Create url to the view just created in web.urls在 web.urls 中创建刚才创建的视图web.urls
  8. Change every links of images, js and css files in index.html to static url as to use in python. Change every links of images, js and css files in index.html to static url as to use in python. {% load static %} is must {% load static %}是必须的
  9. The page will be loaded in localhost now.该页面现在将在 localhost 中加载。
  10. create other views and set urls in web app to other pages like about,contact and so on在 web 应用程序中创建其他views并将urls设置到其他页面,如关于、联系等
  11. Create a list of models You will need and define them in web.models创建模型列表您将需要并在web.models中定义它们
  12. pass them to admin page by using web.admin使用web.admin将它们传递到管理页面
  13. Make it dynamic by passing data from models to templates.通过将数据从模型传递到模板使其动态化。

hope you get it right希望你做对了

There isn't exactly a definite answer: you can organize it however you feel necessary, and Django won't force you to put things in a fixed place.没有确切的答案:您可以根据自己的需要组织它,并且Django 不会强迫您将东西放在固定的地方。 Arguments to back up my point: Arguments 支持我的观点:

  1. Django's template settings: TEMPATES['DIRS'] . Django 的模板设置: TEMPATES['DIRS'] Once you put in a folder named 'templates', it will search through the entire project where a folder named templates is located.放入名为“模板”的文件夹后,它将搜索名为模板的文件夹所在的整个项目。 You can put it on the first level (next to manage.py), or five levels inside an application, it doesn't matter.你可以把它放在第一层(在manage.py旁边),或者一个应用程序内的五个层,没关系。
  2. Django's STATICFILES_DIRS setting. Django 的STATICFILES_DIRS设置。 If Django wants you to put your static files in a directory, it could've limited the arguments to a string, but it defaults to a list and you can stuff in as many as you want.如果 Django 想让您将 static 文件放在一个目录中,它可能会将 arguments 限制为一个字符串,但它默认为一个列表,您可以尽可能多地填充。

To me, I like the way how a book Packt publishes organizes the files: static , site_static , and templates all on the base level.对我来说,我喜欢 Packt 出版的书组织文件的方式: staticsite_statictemplates都在基础级别。

Your application is small now, but once it gets larger, it will be annoying to go through each application to access, say, the templates.您的应用程序现在很小,但是一旦它变大,go 通过每个应用程序访问例如模板会很烦人。 If you keep everything together, you can access all of them easily.如果您将所有内容放在一起,则可以轻松访问所有内容。

So it's something like:所以它是这样的:

|- project
|- app1
|- app2
|- site_static  // your custom static files
    |- css
    |- js
|- static    // bootstrap, webpack stuffs, etc.
|- media     // user uploads and images used in site
|- templates
|- manage.py
|- requirements.txt

Although keep static and media out of the sight though.尽管将staticmedia保持在视线之外。 It's unnecessary to put on github and there'll be privacy/copyright concerns regarding the images.没有必要贴上 github 并且会有关于图像的隐私/版权问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM