본문 바로가기

Python_WEB/Tweetme

[Django]Our First Template

CodingEntrepreneurs Django 강의 정리

render>

from django.shortcuts import render

 

render(request, template_name, context=None, content_type=None, status=None, using=None)

 

1. 지정된 템플릿과 지정된 컨텍스트 사전 결합 및 반환 렌더링된 텍스트가 있는 HttpResponse 객체.

 

DIRS>

1. 템플릿 원본 파일을 찾아야 하는 디렉터리

 

views.py>

# tweets/views.py

from django.http import HttpResponse, Http404, JsonResponse
from django.shortcuts import render

from .models import Tweet

# Create your views here.
def home_view(request, *args, **kwargs):
    return render(request, "pages/home.html", context={}, status=200)


def tweet_detail_view(request, tweet_id, *args, **kwargs):
    """
    REST API VIEW
    Consume by JavaScript or Swift or Java/ios/Andriod
    return json data
    """
    data = {
        "id": tweet_id,
    }
    status = 200

    try:
        obj = Tweet.objects.get(id=tweet_id)
        data["content"] = obj.content
    except:
        data["message"] = "Not Found"
        status = 404

    return JsonResponse(
        data, status=status
    )  # json.dumps content_type='application/json'

 

setting.py>

# tweetme2/settings.py

...
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "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",
            ],
        },
    },
]
...

 

home.html>

<!-- templates/pages/home.html -->

<h1>Hello World Agin</h1> 

 

결과물>

'Python_WEB > Tweetme' 카테고리의 다른 글

[Django]Tweet List View  (0) 2020.07.08
[Django]Bootstrap & Django Templates  (0) 2020.07.08
[Django]Our First Template  (0) 2020.07.08
[Django]Dynamic View into REST API Endpoint  (0) 2020.07.01
[Django]Handling Dynamic Routing  (0) 2020.07.01
[Django]Intro to URL Routing and Dynamic Routing  (0) 2020.06.29