Django Cookbook

VScode tutorial

  • Create a project folder and inside add a project environment for Django
    python -m venv env
  • Install Django in the virtual environment
    python -m pip install django
  • Create a Django Project
    django-admin startproject web_project .

To start the server:

python runserver 1080
  • Create a Django app
    python startapp hello

The folder contains:

  • contains the functions that define pages in your web app
  • contains classes defining your data objects
  • migrations folder: used by Django’s administrative utility to manage database versions
  • app configuration
  • for creating an administrative interface
  • for unit tests

  • add views
    from django.http import HttpResponse
    def home(request):
      return HttpResponse("Hello, Django!")
  • Create a file, hello/ specifies patterns to route different URLs to their appropriate views ```python from django.urls import path from hello import views

urlpatterns = [ path(“”, views.home, name=”home”), ]

map root URL of the app ("") to the view.home function that you just added to hello/

- The web_project folder also contains a file, which is where URL routing is actually handled
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("", include("hello.urls"))
Written on December 7, 2018