Learning Django
Django is a Python-based free and open-source web framework, which follows the model-template-view (MTV) architectural pattern. Django's primary goal is to ease the creation of complex, database-driven websites. The framework emphasizes reusability and "pluggability" of components, less code, low coupling, rapid development, and the principle of don't repeat yourself. Python is used throughout, even for settings files and data models.
A guide to set up Django in a virtual environment
- Create a folder: django-learn
cd django-learn
- (Skip if already installed) Install Python's virtual environment:
sudo apt install virtualenv
and alsosudo apt install python3-dev
- Create a virtual environment in Python:
virtualenv -p python3 env
Virtual environment env created for the directory django-learn
- Activate env:
source env/bin/activate
(env) must appear just prior to the bash prompt
- Install Django for your virtual environment:
pip3 install django
- Create Django project folders:
django-admin startproject demoproj
A single project can have multiple apps
cd demoproj
Inside the demoproj directory
- Create a Django web app:
python3 manage.py startapp demoapp
cd demoapp
- Start the app:
python manage.py runserver
Go to the localhost:8000. If you get to see the page with a green rocket then the Django app is running successfully
Day 1
- Create a view in
demoapp/views.py
- We need to add path in
demoproj/urls.py
for each view we create - Create
static
andtemplates
withindemoapp
directory - Put
html
files inside thetemplates
folder static
folder hasimages
andcss
folders that contains images and css files respectively- Open
demoproj/settings.py
and add your app name'demoapp'
to theINSTALLED_APPS
list and also add this line[os.path.join(BASE_DIR, 'templates')]
to TEMPLATES DIRS
Day 2
- Style
test3.html
using an external CSSstatic/css/style.css
- Style
home.html
using an external CSSstatic/css/home.css
- Put
javascript
files insidestatic/js
- Create a model in
demoapp/models.py
- A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing.
- Generally, each model maps to a single database table.
- Each attribute of the model represents a database field.
- With all of this, Django gives you an automatically-generated database-access API.
- Django’s models provide an Object-relational Mapping (ORM) to the underlying database. ORM is a powerful programming technique that makes working with data and relational databases much easier.
- Refer docs.djangoproject.com
- Refer djangobook.com
- Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.
- Create new migrations based on the changes you have made to your models:
python manage.py makemigrations
- Apply and unapply migrations:
python manage.py migrate
- Refer docs.djangoproject.com
- Create new migrations based on the changes you have made to your models:
- Go to
localhost:8000/admin
for Django admin - To create a user:
python manage.py createsuperuser
- Register your newly created model with the admin
- Refer to Registering models with admin
Day 3
- To view the
Student
model in the form of a table in a browser, we create a html table in a file:showstudents.html
- To create a new entry for a student, we create a html form in
createstudent.html
- To work with forms we need to create a python file:
demoapp/form.py
- Refer Working with forms
{% csrf_token %}
provides cross site request forgery protection. Read this to know more- Each data entry in our model has a unique
id
which is an auto-incremented value. We need to use it whenever we have to uniquely identify each row data.
- To work with forms we need to create a python file:
- To delete an entry, we simply write a function in
views.py
to do that. - To edit an entry, we create a form:
editstudent.html
, fetch the existing data (GET), modify it and submit the changes (POST).- GET: Parameters remain in browser history because they are part of the URL
- POST: Parameters are not saved in browser history.
- Read this to learn more about their difference.
Day 4
- Generating a csv file:
getcsv
method static generates a csv filegetcsv_db
method generates a csv file dynamically. It fetches data from theStudent
model.- Refer Outputting CSV with Django
- Generating a pdf file:
pip3 install reportlab
, we need it to output pdf.getpdf
method static generates a pdf file.getpdf_db
method generates a pdf file dynamically. It fetches data from theStudent
model.getpdf_nice
method generates a pdf file with a well-formatted table that contains data from theStudent
model.- Refer Outputting PDFs with Django
Day 5
- Creating HTML templates:
base.html
includesheader.html
andfooter.html
index.html
andabout.html
extends the templatebase.html
- Added two methods in
views.py
to work with session:setsession
sets sessiongetsession
fetches session
- Added two methods in
views.py
to work with cookies:setcookie
andgetcookie
- Read this to know that difference between session and cookie
- To send email via Django, we need this library
and also add these few lines in
from django.core.mail import send_mail
settings.py
EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.mail.yahoo.com' EMAIL_PORT = 465 EMAIL_HOST_USER = 'edummy17@yahoo.com' EMAIL_HOST_PASSWORD = 'Kolkata@0101'
Day 6
- Authentication in Django
- Creating User account registration page, detailed instructions here
My setup:
- Ubuntu 18.04.3 LTS
- Python 3.6.8
- pip 19.2.2
- virtualenv 16.7.0