create project hello world using django python step by step

create project hello world using django python step by step 1

 Hello world program in django using html( in templates)


  1. download python 3 and install
  2. download vs code editor and install
  3. open vs code editor
press ALT +T and enter 
or

go to terminal menu ->select new terminal
-------------------------------------------------
type following commands
-----------------------------------------------

cd Desktop for desktop path
venv virtual environment support libraries and files
activate virtual environment

then add pip commands to install django
commands in Red

PS C:\Users\sachi> cd Desktop
PS C:\Users\sachi\Desktop> python -m venv myenv

PS C:\Users\sachi\Desktop> myenv\Scripts\activate    

output:
(myenv) PS C:\Users\sachi\Desktop> 

now install django
pip install django

(myenv) PS C:\Users\sachi\Desktop> python -m pip install django


----------------------------------------------------------------

now create project

(myenv) PS C:\Users\sachi\Desktop> django-admin startproject myproject


now go to that folder by cd and create app

(myenv) PS C:\Users\sachi\Desktop\myproject> python manage.py startapp myapp1

------------------------------------------------------
open folder using  VS code editor


directory structure looks like

create folder "templates" under  myapp1



create "index.html" file under templates folder by right click on templates->new file  





you can type html code in index.html

-----------------------------------------------------------
create "urls.py" file under myapp1

note we have 2 urls.py files need one is of myproject and another for myapp

directory structure





views.py    file code  copy paste


from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from django.template import loader

def hello(request):
  template = loader.get_template('index.html')
  return HttpResponse(template.render())




explaination:  above code function hello()   return gives  index.html file 


urls.py  file code      under myapp1


from django.urls import path
from . import views

urlpatterns = [
    path('hello', views.hello, name='hello'),
]


explaination:  above code call function from view by giving url
http://127.0.0.1:8000/myapp1/hello

name= hello  used as shortcut url in html href tag example

<p>Please go <a href="{% url 'hello' %}"> click here </a></p>  


urls.py  file code      under myproject



from django.contrib import admin


from django.urls import include, path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('myapp1/', include('myapp1.urls')),
]


for admin site /admin
for myapp1 site myapp1/hello

if you blank path myapp1
like
 path('', include('myapp1.urls')),

then
url on browser
http://127.0.0.1:8000/hello

--------------------------------------

settings.py file code
just add name of your app in installed_apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp1'
]



-----------------------------------------------------

what we have done
create project
create app
create view
2 urls.py app path in myproject, html web paths in myapp1
settings.py installed app register name of app

and fianlly

run

python manage.py runserver

output





note manage.py file is in myproject directory

so must see path while using manage.py runserver
else use cd command to specify, or back by cd.. command.
Page settings Options