
上QQ阅读APP看书,第一时间看更新
How to do it...
Follow these steps to create a file structure for your project:
- With the virtual environment activated, go to the src directory and start a new Django project, as follows:
(env)$ django-admin.py startproject myproject
The executed command will create a directory called myproject, with project files inside. This directory will contain a Python module, also called myproject. For clarity and convenience, we will rename the top-level directory as django-myproject. It is the directory that you will put under version control, and so it will have a .git or similarly named subdirectory.
- In the django-myproject directory, create a README.md file to describe your project to the new developdjango-admin.py startproject myprojecters.
- The django-myproject directory will also contain the following:
- Your project's Python package, named myproject.
- Your project's pip requirements with the Django framework and other external dependencies (read about this in the Handling project dependencies with pip recipe).
- The project license in a LICENSE file. If your project is open source, you can choose one of the most popular licenses from https://choosealicense.com.
- In your project's root, django-myproject, create the following:
- A media directory for project uploads
- A static directory for collected static files
- A locale directory for project translations
- An externals directory for external dependencies that are included in this project when you can't use the pip requirements
- The myproject directory should contain these directories and files:
- The apps directory where you will put all your in-house Django apps for the project. It is recommended that you have one app called core or utils for the projects' shared functionality.
- The settings directory for your project settings (read about this in the Configuring settings for development, testing, staging, and production environments recipe).
- The site_static directory for project-specific static files.
- The templates directory for the project's HTML templates.
- The urls.py file for the project's URL configuration.
- The wsgi.py file for the project's web server configuration.
- 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 the categorized subdirectories within it. For instance, see the following:
- scss for Sass files (optional)
- css for the generated minified Cascading Style Sheets (CSS)
- img for styling images, favicons, and logos
- js for the project's JavaScriptdjango-admin.py startproject myproject
- vendor for any third-party module combining all types of files, such as the TinyMCE rich-text editor
- Besides the site directory, the site_static directory might also contain overwritten static directories of third-party apps—for example, it might contain cms, which overwrites the static files from Django CMS. To generate the CSS files from Sass and minify the JavaScript files, you can use the CodeKit (https://codekitapp.com/) or Prepros (https://prepros.io/) 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 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 the language chooser.