Book Image

Web Development with Django Cookbook- Second Edition - Second Edition

By : Aidas Bendoraitis
Book Image

Web Development with Django Cookbook- Second Edition - Second Edition

By: Aidas Bendoraitis

Overview of this book

Django is a web framework that was designed to strike a balance between rapid web development and high performance. It has the capacity to handle applications with high levels of user traffic and interaction, and can integrate with massive databases on the backend, constantly collecting and processing data in real time. Through this book, you'll discover that collecting data from different sources and providing it to others in different formats isn't as difficult as you thought. It follows a task-based approach to guide you through all the web development processes using the Django framework. We’ll start by setting up the virtual environment for a Django project and configuring it. Then you’ll learn to write reusable pieces of code for your models and find out how to manage database schema changes using South migrations. After that, we’ll take you through working with forms and views to enter and list data. With practical examples on using templates and JavaScript together, you will discover how to create the best user experience. In the final chapters, you'll be introduced to some programming and debugging tricks and finally, you will be shown how to test and deploy the project to a remote dedicated server. By the end of this book, you will have a good understanding of the new features added to Django 1.8 and be an expert at web development processes.
Table of Contents (18 chapters)
Web Development with Django Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

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

  2. 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). Also, this directory will contain your project's Python package named myproject; Django apps (I 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; a Fabric deployment script named fabfile.py, as suggested in the Creating and using the Fabric deployment script recipe in Chapter 11, Testing and Deployment; and the externals directory for external dependencies that are included in this project if you decide not to use pip requirements.

  3. In your project's Python package, myproject, create the media directory for project uploads, the site_static directory for project-specific static files, the static directory for collected static files, the tmp directory for the upload procedure, and the templates directory for project templates. Also, the myproject directory should contain your project settings, the settings.py and conf directories (read about this in the Configuring settings for development, testing, staging, and production environments recipe), as well as the urls.py URL configuration.

  4. In your site_static directory, create the site 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 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 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 called utils 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