반응형
CodingEntrepreneurs Django 강의 정리
request>
TEMPLATES = [
{
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
],
},
},
]
요청 구문
.user>
TEMPLATES = [
{
"OPTIONS": {
"context_processors": [
"django.contrib.auth.context_processors.auth",
],
},
},
]
일반적으로 contrib.auth의 User 패키지로 설정된다.
.path>
현재 URL 주소
user>
현재 로그인한 사용자
.is_authenticated>
사용자가 인증되었는지 확인
home.html>
{% extends "base.html" %}
{% block content %}
{{ title }}
{{ request.user }}
{{ request.path }}
{{ user }}
{{ user.is_authenticated }}
{% endblock %}
view>
from django.http import HttpResponse
from django.shortcuts import render
from django.template.loader import get_template
# Model View Template (MVT)
# Dont Repeat Yourself = DRY
def home_page(request):
my_title = "Hello there ..."
context = {"title": my_title}
# template_name = "title.txt"
# template_obj = get_template(template_name)
# rendered_string = template_obj.render(context)
# print(rendered_string)
# doc = "<h1>{title}</h1>".format(title=my_title)
# django_render_doc = "<h1>{{ title }}</h1>".format(title=my_title)
# return render(request, "hello_world.html", {"title": rendered_string})
return render(request, "home.html", context)
def about_page(request):
return render(request, "about.html", {"title": "About Us"})
def contact_page(request):
return render(request, "hello_world.html", {"title": "Contact Us"})
def example_page(request):
context = {"title": "Example"}
template_name = "hello_world.html"
template_obj = get_template(template_name)
rendered_item = template_obj.render(context)
return HttpResponse(rendered_item)
적용 스크린샷>
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]Your First App (0) | 2020.06.14 |
---|---|
[Django]Built-In Template Tags (0) | 2020.06.14 |
[Django]Rendering Any Kind of Template (0) | 2020.06.14 |
[Django]Stay DRY with Templates (0) | 2020.06.14 |
[Django]Render Context in Templates (0) | 2020.06.13 |