Learning
- 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)
- -> mkdir projects (make directory)
- -> cd projects (change directory)
- Documentation
- -> django-admin startproject firstproject (firstproject - anyname)
- -> dir firstproject
- -> python manage.py runserver (py provides the live server to run projects - IP generated)
- 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
from django import path from . import views urlpatterns = [path('', views.home, name = 'home')]
from django import HttpResponse def home(request): return HttpResponse("Hello World")
from django.urls import path, include urlpatterns = [path('', include('calc.urls'))]
- --> python manage.py runserver and check the webpage showing Hello World
- Instead of creating static html pages. django provides tempates for creating dynamic pages
- -> In firstproject -> create folder templates -> create file home.html
Hello World!!!
- in settings.py > in templates section
DIRS : [os.path.join(BASE_DIR, 'templates')],
def home(request): #return HttpResponse("Hello World") return render(request,'home.html', {'name': 'Ram'})
Hello {{name}}!!!
- create a base page common for all pages
- templates -> create base.html
- ! and enter will fetch basic code
{% block content %} {% endblock %}
{% extends 'base.html' %} {% block content %} Hello {{name}}!!! {% endblock %}