Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django - First Django Project

Back


Create and Activate Virtual Environment

  1. Create a project directory.

  2. Change the current directory to the project directory

    cd <protject_dir>
    
    • <protject_dir>: the path of project directory.
  3. Create Virtual Environtmet:

    python -m venv <env_name>
    
    • <env_name>: the name of virtual environment.
  4. Activate Virtual Environment:


Install Django in Virtual Environment


Create Django Project


Create Application


Create a View

from django.shortcuts import render
from django.http import HttpResponse    # import HttpResponse from http module

# create an index function taking request as parameter
def index(request):
    return HttpResponse("Hellow world!")  # return a HttpResponse object with a string content.

Create Application URL File

from django.urls import path    # import path function from urls module
from <app_name> import views    # import views module from <app_name> package

# define a list of url patterns
urlpatterns = [
    path("", views.index, name="index"),
  ]


Update Project URL File

from django.contrib import admin
from django.urls import path, include   # import path, include function from urls module

urlpatterns = [
    path('admin/', admin.site.urls),
    path("",include("<app_name>.urls"))
]

Run Project


VS Code: Create Debug File


TOP