- Web Development with Django Cookbook(Second Edition)
- Aidas Bendoraitis
- 672字
- 2021-07-23 14:31:57
Creating a project file structure
A consistent file structure for your projects makes you well-organized and more productive. When you have the basic workflow defined, you can get in the business logic quicker and create awesome projects.
Getting ready
If you haven't done this yet, create a virtualenvs
directory, where you will keep all your virtual environments (read about this in the Working with a virtual environment recipe). This can be created under your home directory.
Then, create a directory for your project's environment, for example, myproject_env
. Start the virtual environment in it. I would suggest adding the commands
directory for local bash scripts that are related to the project, the db_backups
directory for database dumps, and the project
directory for your Django project. Also, install Django in your virtual environment.
How to do it…
Follow these steps in order to create a file structure for your project:
- With the virtual environment activated, go to the project directory and start a new Django project as follows:
(myproject_env)$ django-admin.py startproject myproject
For clarity, we will rename the newly created directory as
django-myproject
. This is the directory that you will put under version control, therefore, it will have the.git
,.svn
, or similar directories. - In the
django-myproject
directory, create aREADME.md
file to describe your project to the new developers. You can also put the pip requirements with the Django version and include other external dependencies (read about this in the Handling project dependencies with pip recipe). Also, this directory will contain your project's Python package namedmyproject
; Django apps (I recommend having an app calledutils
for different functionalities that are shared throughout the project); alocale
directory for your project translations if it is multilingual; a Fabric deployment script namedfabfile.py
, as suggested in the Creating and using the Fabric deployment script recipe in Chapter 11, Testing and Deployment; and theexternals
directory for external dependencies that are included in this project if you decide not to use pip requirements. - In your project's Python package,
myproject
, create themedia
directory for project uploads, thesite_static
directory for project-specific static files, thestatic
directory for collected static files, thetmp
directory for the upload procedure, and thetemplates
directory for project templates. Also, themyproject
directory should contain your project settings, thesettings.py
andconf
directories (read about this in the Configuring settings for development, testing, staging, and production environments recipe), as well as theurls.py
URL configuration. - In your
site_static
directory, create thesite
directory as a namespace for site-specific static files. Then, separate the separated static files in directories in it. For instance,scss
for Sass files (optional),css
for the generated minified Cascading Style Sheets,img
for styling images and logos,js
for JavaScript, and any third-party module combining all types of files such as the tinymce rich-text editor. Besides thesite
directory, thesite_static
directory might also contain overwritten static directories of third-party apps, for example,cms
overwriting static files from Django CMS. To generate the CSS files from Sass and minify the JavaScript files, you can use the CodeKit or Prepros applications with a graphical user interface. - Put your templates that are separated by the apps in your templates directory. If a template file represents a page (for example,
change_item.html
oritem_list.html
), then directly put it in the app's template directory. If the template is included in another template (for example,similar_items.html
), put it in the includes subdirectory. Also, your templates directory can contain a directory calledutils
for globally reusable snippets, such as pagination, language chooser, and others.
How it works…
The whole file structure for a complete project in a virtual environment will look similar to the following:

See also
- The Handling project dependencies with pip recipe
- The Including external dependencies in your project recipe
- The Configuring settings for development, testing, staging, and production environments recipe
- The Deploying on Apache with mod_wsgi recipe in Chapter 11, Testing and Deployment
- The Creating and using the Fabric deployment script recipe in Chapter 11, Testing and Deployment
- Advanced Quantitative Finance with C++
- C語言程序設計基礎與實驗指導
- Unity Virtual Reality Projects
- 算法精粹:經典計算機科學問題的Python實現
- Android 應用案例開發大全(第3版)
- Highcharts Cookbook
- SQL經典實例(第2版)
- Python算法詳解
- HTML+CSS+JavaScript編程入門指南(全2冊)
- Hadoop 2.X HDFS源碼剖析
- Python數據可視化之美:專業圖表繪制指南(全彩)
- Unity 5.X從入門到精通
- Android移動應用開發項目教程
- Software Development on the SAP HANA Platform
- C語言開發寶典