Tutorial by Examples: django

Supposing you have setup a django project, and the settings file is in an app named main, this is how you initialize your code import os, sys # Setup environ sys.path.append(os.getcwd()) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings") # Setup django imp...
django-filter is generic system for filtering Django QuerySets based on user selections. The documentation uses it in a function-based view as a product model: from django.db import models class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(...
Our UserProfile class Create a UserProfile model class with the relationship of OneToOne to the default User model: from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save class UserProfile(models.Model): user = models.OneTo...
Django is a full stack, feature rich web development framework. It bundles a lot of functionality together to provide a common, quick and productive experience for web developers. Django projects consist of common settings, and one or more applications. Each application is a set of functionality al...
Three basic tools. nginx - free, open-source, high-performance HTTP server and reverse proxy, with high performance; gunicorn - 'Green Unicorn' is a Python WSGI HTTP Server for UNIX (needed to manage your server); supervisor - a client/server system that allows its users to monitor and control ...
First of all we need to add MEDIA_ROOT and MEDIA_URL to our settings.py file MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' Also here you will work with ImageField, so remember in such cases install Pillow library (pip install pillow). Otherwise, you will have such error: I...
Django Model Form with Django Class Based view is a classic way of building pages to do create/update operations in django application quickly. Within the form we can put methods to execute tasks. Its a cleaner way to put tasks in forms rather than putting in views/models. To give an example using ...
Make a simple Hello World Example using your django. let's make sure that you have django installed on your PC first. open a terminal and type: python -c "import django" -->if no error comes that means django is already installed. Now lets create a project in django. For that write ...
Internally, Django uses the Python logging system. There is many way to configure the logging of a project. Here is a base: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'default': { 'format': "[%(asctime)s] %(levelname)s [%(n...
Install pip : $ pip install django-cassandra-engine Add Getting Started to INSTALLED_APPS in your settings.py file: INSTALLED_APPS = ['django_cassandra_engine'] Cange DATABASES setting Standart: Standart DATABASES = { 'default': { 'ENGINE': 'django_cassandra_engine', ...
Drop/Delete your database If you are using SQLite for your database, just delete this file. If you are using MySQL/Postgres or any other database system, you will have to drop the database and then recreate a fresh database. You will now need to delete all the migrations file except "init.py...
For all my projects, Django-Allauth remained one that is easy to setup, and comes out of the box with many features including but not limited to: Some 50+ social networks authentications Mix signup of both local and social accounts Multiple social accounts Optional instant-signup for social ac...
We can perform a GROUP BY ... COUNT or a GROUP BY ... SUM SQL equivalent queries on Django ORM, with the use of annotate(), values(), order_by() and the django.db.models's Count and Sum methods respectfully: Let our model be: class Books(models.Model): title = models.CharField() ...
from django.contrib.auth.models import User class UserAdmin(admin.ModelAdmin): list_display = ('email', 'first_name', 'last_name') list_filter = ('is_staff', 'is_superuser') admin.site.unregister(User) admin.site.register(User, UserAdmin) We need to unregister before regis...
When using Django as a web service framework, the package django-log-request-id can be used to parse and log request IDs. Settings MIDDLEWARE_CLASSES = ( 'log_request_id.middleware.RequestIDMiddleware', # ... other middleware goes here ) LOGGING = { 'version': 1, 'disable_e...
Django Views are simply the functions that get called when a request is made to a certain URLs. URL patterns are written in urls.py file, each URL regex is given a function(Django view) from a views.py, so when a request is made, that function gets the call, with the HTTP request object, and then y...
A Django model typically refers to a table in the database, attributes of that model becomes the column of that table. In more of a real-world example, you would create a model for any entity in your application, and store its attributes with django fields which automatically handles data-types conv...
A simple example would be for a library management application; you would have 2 models, for example, student and book in models.py: from django.db import models class student(models.Model): roll_no = models.IntegerField(primary_key=True) first_name = models.CharField(max_length=30) ...

Page 2 of 2