Skip to content

kjs29/Django

Repository files navigation

This repository is for creating a website using Django

What is Django?

Django is free and open source web application framework, written in Python.

Frameworks exist to save you from having to reinvent the wheel.

Source

Django follows this MVT(MODEL VIEW TEMPLATE) structure.

basic-django

Source : Mozilla website

Things I learned.

.

.

.

- DRY is important but understanding core concepts of Django such as MVT architecture (I like to call it MVT since View is in the middle), URL routing, basic database management, should come first for beginners like me.

- Learning Class based views without understanding how Django works is like building a castle on sand. Not a good solid way to start. I am a beginner.

- views.pyhandles HTTP REQUESTS. Request -> Response

# What views.py can do
# Pull data from database
# Transform data
# Send email
# Anything that is possible...

- views.py is the core engine of Django. It is literally like a function. It takes input and returns an output. Practice function based view first to understand how view works.

- Django Template Language is important but it is intended to avoid advanced logic. Even Django Document mentions that it shouldn't be assumed as Python knowledge (https://docs.djangoproject.com/en/4.1/misc/design-philosophies/#don-t-invent-a-programming-language)

- How to add a new url

Click to expand

Let's say I want to add a new url address(/profile) to my website called lawyer.com like lawyer.com/profile

Assuming that we have created a project already,

On a project root directory, first I need to create an app called profile_main

django-admin startapp profile_main
  1. Go to urls.py in the project folder (NOT an app folder), import include function and add path to urls.py in profile_main(app folder)
from django.contrib import admin

# include include after path
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),

    # now a new URL 'lawyer.com/profile' is added,
    # it will search related sub URLS in the file profile_main/urls.py
    # this does NOT mean 'lawyer.com/' will work.
    path("profile/", include("profile_main.urls"))
]
  1. Create a urls.py in the app folder called profile_main

profile_main (app folder)/urls.py

from django.urls import path

# from the current directory import views.py
from . import views

urlpatterns = [
    # always put / at the end of route

    # new URL(lawyer.com/profile) is added
    path("",views.profile_main),

    # new URL(lawyer.com/profile/jin) is added
    path("jin/", views.jin)
]
  1. Go to views.py in the profile_main folder, and decides what to show

profile_main / views.py

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

# Create your views here
def profile_main(request):
    return HttpResponse("lawyer.com/profile")
def jin(request):
    return HttpResponse("lawyer.com/profile/jin")

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages