Django

Learning

Table of Contents

Configurations

  • Install Python
  • To work on virtual environment
    • cmd commands -> pip install virtualenvwrapper-win
    • -> mkvirtualenv test (test - anyname)
  • install Django
    • -> pip install django (check version django-admin --version)

Create_Project

Create working directory

  • -> mkdir projects (make directory)
  • -> cd projects (change directory)

New project and sample run

  • Documentation
  • -> django-admin startproject firstproject (firstproject - anyname)
  • -> dir firstproject
  • -> python manage.py runserver (py provides the live server to run projects - IP generated)

Hello_World

  • use Visual studio code IDE (Integreated Development Environment)
  • Open folder (projects -> firstproject) created already.
  • Go to Terminal check -> django --version if not installed -> pip install django
  • -> workon test
  • Inside firstproject -> python manage.py startapp calc (new project created)
  • Inside calc -> create newfile urls.py

In calc -> urls.py

from django import path
from . import views
urlpatterns = [path('', views.home, name = 'home')]

In calc -> views.py

from django import HttpResponse
def home(request):
  return HttpResponse("Hello World")

In firstproject -> urls.py

from django.urls import path, include
urlpatterns = [path('', include('calc.urls'))]
  • --> python manage.py runserver and check the webpage showing Hello World

Django_Template_Language

  • Instead of creating static html pages. django provides tempates for creating dynamic pages
  • -> In firstproject -> create folder templates -> create file home.html

home.html

Hello World!!!
  • in settings.py > in templates section
DIRS : [os.path.join(BASE_DIR, 'templates')],

In calc -> views.py

def home(request):
    #return HttpResponse("Hello World")
    return render(request,'home.html', {'name': 'Ram'})

In templates -> home.html

Hello {{name}}!!!
  • create a base page common for all pages
  • templates -> create base.html

base.html

  • ! and enter will fetch basic code
    {% block content %}
    {% endblock %}

in home.html

{% extends 'base.html' %}
{% block content %}
Hello {{name}}!!!
{% endblock %}