Install Django
Before you can use Django, you’ll need to install it. Our complete installation guide covers all the possibilities; this guide will get you to a simple, minimal installation that’ll work while you walk through the introduction.
For linux :
pip install django
For Windows :
python -m pip install django
Verify installation
on python terminal we will write
>>> print(django.get_version())2.1
Write your first Django app
Open command prompt to create new django project we should write on terminal the following command :
>> django-admin startproject firstdemoproject
now we will move to the folder and create a new app inside project folder
>>cd firstdemoproject >> django-admin startapp app1
it will create required folders inside the directory structure.
Register your App
â??â??â??â??manage.pyâ??â??â??â??app1â?? settings.pyâ?? urls.pyâ?? wsgi.pyâ?? __init__.pyâ??â??â??â??requirements.txt
find the file with name settings.py
and inside that file look for following code :
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles','app1']
add your app name as the last entry in the file and save and close the file.
A view is a place where we put the "logic" of our application, a webpage is basically a view in django.
Adding code to views.py
from django.shortcuts import renderfrom django.http import request,HttpResponsedef homepage(request): return HttpResponse("this is first page")
save and close the file.
Adding url to urls.py
from app1.views import homepageurlpatterns = [ path('admin/', admin.site.urls), path('homepage/',homepage)]
now to Run the program type the following command at command prompt as folows :
>>python manage.py runserver
while your program is in running state ,open your browser and open url as follows :
http://localhost:8000/homepage
it will display your very first django app.