Django is a high-level web framework that is written in Python. It is designed to help developers build web applications quickly and efficiently. Django is known for its “batteries included” philosophy, which means that it comes with a lot of built-in features and tools that make it easy to get started with web development.
One of the key features of Django is its ability to handle complex web applications with ease. It provides a powerful ORM (Object-Relational Mapping) system that allows developers to interact with databases using Python code. This makes it easy to create, read, update, and delete data from a database without having to write complex SQL queries.
Django also comes with a built-in admin interface that makes it easy to manage data and content on a website. This interface can be customized to fit the needs of a specific project, and it can be used to manage users, groups, permissions, and more.
Another key feature of Django is its support for reusable apps. Developers can create standalone apps that can be used in multiple projects, which can save a lot of time and effort. There are also many third-party apps available that can be used to add additional functionality to a Django project.
Overall, Django is a powerful and flexible web framework that is well-suited for building complex web applications. Its built-in features and tools make it easy to get started with web development, and its support for reusable apps and third-party libraries make it easy to add additional functionality to a project.
Cheat Sheet
Django Cheat Sheet | Description |
Installation | |
pip install django | Install Django using pip |
django-admin startproject projectname | Create a new Django project |
python manage.py runserver | Start the development server |
Models | |
class ModelName(models.Model): | Define a new model |
models.CharField(max_length=100) | Define a character field |
models.IntegerField() | Define an integer field |
models.BooleanField() | Define a boolean field |
models.ForeignKey(‘OtherModel’) | Define a foreign key |
Views | |
def view_name(request): | Define a new view |
return render(request, ‘template.html’, context) | Render a template |
return HttpResponse(‘Hello, world!’) | Return a plain text response |
Templates | |
{% extends ‘base.html’ %} | Extend a base template |
{% block content %} {% endblock %} | Define a content block |
{{ variable }} | Output a variable |
{% for item in list %} {% endfor %} | Loop over a list |
Forms | |
class FormName(forms.Form): | Define a new form |
forms.CharField(max_length=100) | Define a character field |
forms.IntegerField() | Define an integer field |
forms.BooleanField() | Define a boolean field |
Authentication | |
from django.contrib.auth import authenticate, login, logout | Import authentication functions |
user = authenticate(username=username, password=password) | Authenticate a user |
login(request, user) | Log in a user |
logout(request) | Log out a user |
Admin | |
from django.contrib import admin | Import the admin module |
admin.site.register(ModelName) | Register a model with the admin |
Static Files | |
{% load static %} | Load the static files template tag |
<link rel=”stylesheet” href=”{% static ‘style.css’ %}”> | Link to a static CSS file |
<img src=”{% static ‘image.png’ %}” alt=”Image”> | Display a static image |
URLs | |
from django.urls import path | Import the path function |
path(‘url/’, view_name, name=’name’) | Define a new URL |
{% url ‘name’ %} | Reverse a URL |
Middleware | |
class MiddlewareName: | Define a new middleware |
def __init__(self, get_response): | Define the middleware constructor |
def __call__(self, request): | Define the middleware logic |
Settings | |
DEBUG = True | Enable debug mode |
ALLOWED_HOSTS = [‘example.com’] | Set allowed hosts |
DATABASES = {…} | Configure the database |
Deployment | |
pip install gunicorn | Install Gunicorn |
gunicorn projectname.wsgi | Start Gunicorn |
python manage.py collectstatic | Collect static files |
Testing | |
python manage.py test appname | Run tests for an app |
assertEqual(a, b) | Assert that a equals b |
assertContains(response, text) | Assert that text is in the response |
Internationalization | |
from django.utils.translation import gettext as _ | Import the translation function |
{% trans “Hello, world!” %} | Translate a string |
LANGUAGE_CODE = ‘en-us’ | Set the default language |
Caching | |
from django.core.cache import cache | Import the cache |
cache.set(‘key’, ‘value’, timeout=60) | Set a cache value |
cache.get(‘key’) | Get a cache value |
REST Framework | |
pip install djangorestframework | Install the REST framework |
from rest_framework import serializers, viewsets | Import the serializers and viewsets |
class SerializerName(serializers.ModelSerializer): | Define a new serializer |
class ViewSetName(viewsets.ModelViewSet): | Define a new viewset |