Creating an application for blogging using DJango
Steps followed :
- pip install django
Collecting django ......etc..... Successfully installed django-2.2.6 sqlparse-0.3.0
- python -m django --version
2.2.6 => This shows django was installed properly and shows the version
- django-admin startproject bloggers_adda_project
=> This creates a folder by name bloggers_adda_project along with contents needed for the project. This folder would contain manage.py file and another folder inside by the same name as the project folder name i.e. "bloggers_adda_project".
- code .
=> To open the project in VS Code editor.
- cd bloggers_adda_project
=> To move one level/directory/folder inside the created project folder to execute the runserver command.
- python manage.py runserver
.......etc... Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
=> This code runs the server using the manage.py file. Usually the port would be http://127.0.0.1:8000/ for Django projects.
- Open web browser window and go to http://127.0.0.1:8000/ - we should be able to see django default web page which says "The install worked successfully! Congratulations!"
[You may need to stop server with <ctrl + c> before the next step] 8) python manage.py startapp bloggers_adda
=> This creates a new App inside the project that we just created. A project can have multiple Apps running. Hence this step.
- views.py ==>>
Add code :
from django.http import HttpResponse
def home(request):
return HttpResponse('<h1>Bloggers Adda Home Page</h1>')
- Create urls.py file in bloggers_adda (app folder) and add code :
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='bloggers_adda_home'),
]
- bloggers_adda_project/urls.py ==>>
Modify line :
from django.urls import path
to
from django.urls import path, include
and to the urlpatterns list, add another line :
path('bloggers_adda/', include('bloggers_adda.urls')),
- python manage.py runserver
=> To run the server again after all the file modifications.
- Browsing http://127.0.0.1:8000/ would now give a 404 page not found error since it does not match any url pattern that we created. Hence, we may have to try :
or
http://127.0.0.1:8000/bloggers_adda/
- Modify views.py file in bloggers_adda ==>>
Add code :
def about(request):
return HttpResponse('<h1>Bloggers Adda About Page</h1>')
- Modify urls.py file in bloggers_adda ==>>
Add line to urlpatterns list :
path('about/', views.about, name='bloggers_adda_about'),
-
Inside the bloggers_adda (app folder), create a folder by name 'templates'.
-
Create home.html, about.html, layout.html etc... and add the requied html codes.
-
bloggers_adda/apps.py ==>>
Copy class name : 'BloggersAddaConfig'
- bloggers_adda_project/settings.py ==>>
Add line on top of the INSTALLED_APPS list
INSTALLED_APPS = [
'bloggers_adda.apps.BloggersAddaConfig',
- bloggers_adda/views.py ==>>
Instead of return HttpRequest line, code :
return render(request, 'bloggers_adda/home.html')
{% load static %} # => has been used on base.html even before <!DOCTYPE html> which is to be noted in Django
Also used :
{% static 'bloggers_adda/main.css' %} # => for stylesheet href
{% url 'bloggers_adda_home' %} # => for href link to home page instead of hardcoding /home/
{% url 'bloggers_adda_home' %} # => for href link to about page instead of hardcoding /about/