Django is a library of reusable modules to build a fast scaleable website with Python.
It will help us handle HTTP requests, cookies etc
Django will determine the structure of our project also
We will also try to deploy our django site to Cloudflare
Setup
Run the following in your terminal
$pipinstalldjango==4.2
Generate Project
Run the following in the terminal
$django-adminstartprojectapp.
Running the Django Dev Server
Once running the dev server should be available here by default
Here we will run a specific command from manage.py called runserver
This is similar to scripts in node package.json file
$pythonmanage.pyrunserver
Add additional Apps to the project
startapp is another command for adding new apps
$pythonmanage.pystartappwebsite
Working on new app
In our new website folder we will create a view and add routing
Creating the new View
Open the website/views.py file in you IDE
Add the following
from django.http import HttpResponse # Here we import the HttpResponse methodfrom django.shortcuts import render# Create your views here.defindex(request): # All default views have the name index although not required it is the standard naming conventionreturn HttpResponse('gikken.com algos') # We then return a string for the moment
Adding routing to the website app
Create a new file in website called urls.py
Add the following
from django.urls import path # We get the path method from . import views # We import from the local dir to make sure we dont import an external views packageurlpatterns = [ # All routing url files must have the name urlpatterns path('', views.index) # We then point the route to the view function, note it is uncalled here, also empty string is the top root]
Add top level routing
We then update the top level routing in app/urls.py
Add the following
from django.contrib import adminfrom django.urls import path, include # We import the include methodurlpatterns = [ path('admin/', admin.site.urls), path('', include('website.urls')) # We point the top level route to our new app urls file, this will pick up the urlpatterns object]
Add new Page
We will add the full page output
In this site it will show algos
Updating views.py
from django.http import HttpResponsefrom django.shortcuts import render# Create your views here.defindex(request):return HttpResponse('gikken.com algos')deffull_page(request): # We add this new functionreturn HttpResponse('Full Page Layout') # Just return some simple text for now