Django Overview
Framework basic summary
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
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
- preparing and restructuring data to make it ready for rendering
- creating HTML forms for the data
- receiving and processing submitted forms and data from the client
Most of the forms in Django should be created from models, using ModelForms, Model Fields and CSRF protection.
ModelForms are useful to:
- generate HTML
- use built-in validators approriate to each field.
forms.ModelForm.is_valid()
A ModelForm maps fields from model classes to HTML form <input>
elements via a the Form class.
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
- https://docs.djangoproject.com/en/1.9/topics/testing/
- Assertions available are from unittest and from Django’s TestCase so the full list of assertions available are:
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
Related Links
- Testing tutorial https://docs.djangoproject.com/en/1.9/topics/testing/
Admin
- Edit models on the same page as a parent model with Model Inlines https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects
References
- Two Scoops Of Django
- Official Documentation in categories https://docs.djangoproject.com/en/
- Single page with links to each doc https://docs.djangoproject.com/en/1.9/contents/
*[ORM]: Object Relational Mapping
- August 1, 2023
- How to create a reusable Django app and distribute it with PIP or publish to pypi.orgJune 29, 2021
- How To Serve Multiple Django Applications with uWSGI and Nginx in Ubuntu 20.04October 26, 2020
- How to add favicon to Django in 4 stepsSeptember 3, 2020
- Categories in Django with BreadcrumbsAugust 30, 2020
- How To Migrate From SQLite To PostgreSQL In Django In 3 stepsAugust 28, 2020
- Practical guide to internationalize a Django app in 5 steps.August 24, 2020
- Disable new users singup when using Django's allauth packageSeptember 3, 2019
- How to add ads.txt to Django as requested by Google AdsenseAugust 30, 2019
- Have multiple submit buttons for the same Django formJuly 2, 2019
- Better Testing with Page Object Design in DjangoMay 1, 2019
- Generating slugs automatically in Django without packages - Two easy and solid approachesFebruary 14, 2019
- How to set up Django tests to use a free PostgreSQL database in HerokuFebruary 13, 2019
- Dynamically adding forms to a Django FormSet with an add button using jQueryFebruary 6, 2019
- Use of Django's static templatetag in css file to set a background imageFebruary 1, 2019
- Activate Django's manage.py commands completion in Bash in 2 stepsJanuary 29, 2019
- Sending Emails with Django using SendGrid in 3 easy stepsJanuary 9, 2019
- Adding Users to Your Django Project With A Custom User ModelSeptember 21, 2018
- Setting Up A Factory For One To Many Relationships In FactoryboyApril 17, 2018
- Generate UML class diagrams from django modelsMarch 24, 2018
- Set Up Ubuntu To Serve A Django Website Step By StepJuly 3, 2017
- Django Project Directory StructureJuly 16, 2016
- How to Have Different Django Settings for Development and Production, and environment isolationJune 10, 2016
- Django Overview
Django Forms
- Adding a Cancel button in Django class-based views, editing views and formsJuly 15, 2019
- Using Django Model Primary Key in Custom Forms THE RIGHT WAYJuly 13, 2019
- Django formset handling with class based views, custom errors and validationJuly 4, 2019
- How To Use Bootstrap 4 In Django FormsMay 25, 2018
- Understanding Django FormsApril 30, 2018
- How To Create A Form In DjangoJuly 29, 2016
Articles
Subcategories
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
·