Monday, 5 July 2021

*Episode 7* PYTHON (Creating a Hello World App with Django)


Creating a Hello World app


This episode covers:-


 1.) How to create apps.

 2.) Introduction on views, paths and templates.


  •  Setup :-

Terminal

cp -fr 08-Django-Project 09-Hello-World

cd 09-Hello-World

source ../venv/bin/activate


  •  Creating apps :-

Use startapp command to create a new app:

Terminal

python manage.py startapp myapp


Now you should have this kind of folder structure:

Folder structure

projects

├── 08-Django-Project

├── 09-Hello-World

│ ├── db.sqlite3

│ ├── manage.py

│ ├── myapp # < new app

│ └── mysite

└── venv


      Edit mysite app settings.py file and add myapp to the INSTALLED_APPS list:


mysite/settings.py

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'myapp', # < here

]


  •  Creating template files :-

     Create index.html file in the myapp templates folder. You have to create the templates and myapp folders too:

Folder structure for templates

├── 09-Hello-World

│ ├── db.sqlite3

│ ├── manage.py

│ ├── myapp

│ │ ├── templates <-- here

│ │ │ └── myapp <-- here

│ │ │ └── index.html <-- here


     Add this HTML markup inside the index.html file:

myapp/templates/myapp/index.html

<h1>Hello world! I was brought to you by the myapp index vi\

ew.</h1>


Creating views :-


      Edit myapp app views.py file and add an index function:

myapp/views.py

from django.shortcuts import render

def index(request): # < here

return render(request, 'myapp/index.html')


  •  Adding a homepage path :-

       Edit mysite app urls.py file add the index path to the urlpatterns list:

mysite/urls.py

from django.contrib import admin

from django.urls import path

from myapp import views as myapp_views # < here

urlpatterns = [

path('admin/', admin.site.urls),

path('', myapp_views.index, name='index'), # < here

]


      Run the development server:

Terminal

python manage.py runserver




     Visit http://127.0.0.1:8000 and you should see this:




       We will deepen the knowledge about templates, views and paths as we go along.


Summary


  •  startapp command creates new apps.
  •  Don’t forget to add the app to the mysite/settings.py file INSTALLED_APPS list.
  •  app/templates/app/ is a typical location for app template files.
  •  app/views.py file is a typical location for app view functions.
  •  mysite/urls.py file is a typical location for URL patterns.


👈Episode 6(P).                                                                                  Episode 8(P)👉
Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...