Django Overview

Framework basic summary

Published:
Last modified:

Concepts

Each Django project can be composed of many apps. An app is a small project that should focus on performing one task (e.g. A website project can have: a blog, ratings, news apps).

Django project structure

A typical Django project, at first, has a very basic layout. After following Django docs suggestions, like creating the project $ django-admin startproject my_project and some app $ python manage.py startapp my_app will create the following structure:

.
└── mysite/
    β”œβ”€β”€ manage.py
    β”œβ”€β”€ mysite/
    |   β”œβ”€β”€ __init__.py
    |   β”œβ”€β”€ settings.py
    |   β”œβ”€β”€ urls.py
    |   └── wsgi.py
    └── myapp/
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ admin.py
        β”œβ”€β”€ apps.py
        β”œβ”€β”€ migrations/
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ models.py
        β”œβ”€β”€ tests.py
        β”œβ”€β”€ urls.py
        └── views.py
Default settings are located in django/conf/global_settings.py, they are overwritten in mysite/settings.py

The structure that sets up Django by default is very basic, when a project starts to grow, it starts to require a better approach, outlined in Django directory structure that deals with other aspects of development and deployment such as:

  • deployment scripts
  • separated tests by units
  • having different environments for development, production (staging)
  • documentation for the project

Models

Fat models, thin views.

Views

Templates

Forms

In a Django web app, a form can refer to:

  • an HTML form a component of a Web page that has form controls
  • a Django Form that produces an HTML form
  • the structured data returned when a form is submitted
  • all of the above interacting together
Django handles three distinct parts of the work involved in forms
  1. preparing and restructuring data to make it ready for rendering
  2. creating HTML forms for the data
  3. receiving and processing submitted forms and data from the client
Djangoproject.com tutorial

Most of the forms in Django should be created from models, using ModelForms, Model Fields and CSRF protection.

ModelForms are useful to:

A ModelForm maps fields from model classes to HTML form <input> elements via a the Form class.

One of the core features of Django Forms is that they make it easy to validate all the data.

Form methods

If the form is used to retrieve data, it should use the GET method, if it modifies data, it needs to use the POST method.

Reference: https://docs.djangoproject.com/en/1.9/topics/forms/

Testing

The preferred way to write tests in Django is using the unittest module built in to the Python standard library

In Django we subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation.

Django provides a test Client to simulate a user interacting with the code at the view level. We can use it in tests.py

Admin

References

*[ORM]: Object Relational Mapping

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·