Tutorial by Examples: django

django-admin is a command line tool that ships with Django. It comes with several useful commands for getting started with and managing a Django project. The command is the same as ./manage.py , with the difference that you don't need to be in the project directory. The DJANGO_SETTINGS_MODULE envir...
Django ORM is a powerful abstraction that lets you store and retrieve data from the database without writing sql queries yourself. Let's assume the following models: class Author(models.Model): name = models.CharField(max_length=50) class Book(models.Model): name = models.CharField(max...
Install gunicorn pip install gunicorn From django project folder (same folder where manage.py resides), run the following command to run current django project with gunicorn gunicorn [projectname].wsgi:application -b 127.0.0.1:[port number] You can use the --env option to set the pat...
Forms can be defined, in a similar manner to models, by subclassing django.forms.Form. Various field input options are available such as CharField, URLField, IntegerField, etc. Defining a simple contact form can be seen below: from django import forms class ContactForm(forms.Form): contac...
Assuming a class from django.db import models class Author(models.Model): name = models.CharField(max_length=50) def __str__(self): return self.name def get_absolute_url(self): return reverse('view_author', args=[str(self.id)]) class Book(models.Mo...
tl;dr : Create a base class that defines two user objects (say user and another_user). Create your other models and define three Client instances. self.client : Representing user logged in browser self.another_client : Representing another_user 's client self.unlogged_client : Representing unlo...
You can get rid of manage.py and use the django-admin command instead. To do so, you will have to manually do what manage.py does: Add your project path to your PYTHONPATH Set the DJANGO_SETTINGS_MODULE export PYTHONPATH="/home/me/path/to/your_project" export DJANGO_SETTINGS_MODULE...
Django handles a request by routing the incoming URL path to a view function. The view function is responsible for returning a response back to the client making the request. Different URLs are usually handled by different view functions. To route the request to a specific view function, Django look...
With the Class Based generic Views, it is very simple and easy to create the CRUD views from our models. Often, the built in Django admin is not enough or not preferred and we need to roll our own CRUD views. The CBVs can be very handy in such cases. The CreateView class needs 3 things - a model, t...
Everything you need to get started with Django admin is already setup in Django's default project layout. This includes: # settings.py # `django.contrib.admin` and its dependancies. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes',...
Configure your app's URLconf to automatically use a URL namespace by setting the app_name attribute: # In <myapp>/urls.py from django.conf.urls import url from .views import overview app_name = 'myapp' urlpatterns = [ url(r'^$', overview, name='overview'), ] This will set t...
The query attribute on queryset gives you SQL equivalent syntax for your query. >>> queryset = MyModel.objects.all() >>> print(queryset.query) SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel" Warning: This output should only be used for d...
One potential implementation of Redis as a backend caching utility is the django-redis-cache package. This example assumes you already have a Redis server operating. $ pip install django-redis-cache Edit your settings.py to include a CACHES object (see Django documentation on caching). CACHES ...
One potential implementation of Redis as a backend caching utility is the django-redis package. This example assumes you already have a Redis server operating. $ pip install django-redis Edit your settings.py to include a CACHES object (see Django documentation on caching). CACHES = { 'de...
If you plan to host your Django website on Heroku, you can start your project using the Heroku Django Starter Template : django-admin.py startproject --template=https://github.com/heroku/heroku-django-template/archive/master.zip --name=Procfile YourProjectName It has Production-ready configurati...
static and templates folder in the apps may should also contain a folder with the name of app ex. blog this is a convention used to prevent namespace pollution, so we reference the files like /blog/base.html rather than /base.html which provides more clarity about the file we are referencing and pre...
One big problem is that valuable named routes is not supported by Express out of the box. Solution is to install supported third-party package, for example express-reverse: npm install express-reverse Plug it in your project: var app = require('express')(); require('express-reverse')(app); ...
First, you need to install django-debug-toolbar: pip install django-debug-toolbar settings.py: Next, include it to project's installed apps, but be careful - it's always a good practice to use a different settings.py file for such development-only apps and middlewares as debug toolbar: # If en...
Django 1.10 introduced a new middleware style where process_request and process_response are merged together. In this new style, a middleware is a callable that returns another callable. Well, actually the former is a middleware factory and the latter is the actual middleware. The middleware facto...
Following are the Prerequisites for installing Cookiecutter: pip virtualenv PostgreSQL Create a virtualenv for your project and activate it: $ mkvirtualenv <virtualenv name> $ workon <virtualenv name> Now install Cookiecutter using: $ pip install cookiecutter Change dire...

Page 1 of 2