320x100
cbv는 class based view 라는 뜻이다.
# 첫번째 파일
school > classroom > templates > classroom > home.html
<h1>welcome to home.html</h1>
<li>
<ul>
<a href='{% url 'classroom:thank_you' %}'>Thank you page link</a>
</ul>
</li>
# 두번째 파일
school > classroom > views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
# def home_view(request):
# return render(request, 'classroom/home.html')
class HomeView(TemplateView):
template_name = 'classroom/home.html'
class ThankYouView(TemplateView):
template_name = 'classroom/thank_you.html'
# 세번째 파일
school > classroom > urls.py
from django.urls import path
from .views import HomeView, ThankYouView
app_name = 'classroom'
# domain.com/classroom/
urlpatterns = [
path('', HomeView.as_view(), name = 'home'), # path expects a function
path('thank_you/', ThankYouView.as_view(), name = 'thank_you'), # path expects a function
]
# 네번째 파일
school > classroom > templates > classroom > thank_you.html
<h1>Thank you</h1>
# 다섯번째 파일
school > school > urls.py
"""school URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path('classroom/', include('classroom.urls'))
]
# 여섯번째 파일
school > school > settings.py
"""
Django settings for school project.
Generated by 'django-admin startproject' using Django 4.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-r8xy*(trd!8zvbx)c11$ilz7_-tsnxp1nz5%osv)1x*3d_a==t"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'classroom.apps.ClassroomConfig',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "school.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "school.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# 일곱번째 파일
school > classroom > models.py
from django.db import models
# Create your models here.
class Teacher(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
subject = models.CharField(max_length=30)
def __str__(self):
return f'{self.first_name}{self.last_name} teaches {self.subject}'
300x250
'개발일지 > 임시카테고리' 카테고리의 다른 글
Django - cbv CreateView (0) | 2022.10.01 |
---|---|
Django - cbv formview (0) | 2022.10.01 |
Django - ModelForms 커스터마이징 (0) | 2022.10.01 |
Django - ModelForms (0) | 2022.10.01 |
Django - 위젯과 스타일링 (0) | 2022.10.01 |