Django 2 Web Development Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Follow these steps in order to create a file structure for your project:

  1. 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 django-myproject. This is the directory that you will put under version control, therefore, it will have .git, .svn, or similar subdirectories.

  1. In the django-myproject directory, create a README.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).
  2. The django-myproject directory will also contain the following:
    • Your project's Python package, named myproject
    • Django apps (we recommend having an app called utils for different functionalities that are shared throughout the project)
    • A locale directory for your project translations if it is multilingual
    • The externals directory for external dependencies that are included in this project if you decide not to use pip requirements
  1. In your project's root, django-myproject. Create the following:
    • A media directory for project uploads
    • A site_static directory for project-specific static files
    • A static directory for collected static files
    • A tmp directory for the upload procedure
    • templates directory for project templates
  2. The myproject directory should contain your project settings in settings.py and a config directory (read about this in the Configuring settings for development, testing, staging, and production environments recipe), as well as the urls.py URL configuration.
  3. In your site_static directory, create the site directory as a namespace for site-specific static files. Then, we will divide the static files between categorized subdirectories in it. For instance, see the following:
    • scss for Sass files (optional)
    • css for the generated minified Cascading Style Sheets (CSS)
    • 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
  4. Besides the site directory, the site_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.
  5. 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 or item_list.html), then put it directly 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 called utils for globally reusable snippets, such as pagination and language chooser.