본문 바로가기

Python_WEB/Try_Django

[Django]Include with Arguments

반응형

CodingEntrepreneurs Django 강의 정리

with>

1. foreign key 관계에서 사용. 첨부된 모든 코멘트를 반복

 

list-inline.html>

<div class="col-12 col-md-10 mb-3 mx-auto">
    <div class='card'>
        <div class='card-body'>
            <h5 class="card-title">{{ blog_post.title }}</h5>
            <p class="cart-text">{{ blog_post.content }}</p>
        </div>
    </div>
</div>

 

list.html>

{% extends "base.html" %}

{% block content %}

<div class="row">
    {% for object in object_list %}
    {% include 'blog/list-inline.html' with blog_post=object %}
    {% endfor %}
</div>

{% endblock %}

 

base.html>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>{% block head_title %} Replace this value {% endblock %} CFE</title>
</head>

<body>
    {% include 'navbar.html' %}

    <div class="container">
        <!-- <div class="row"> -->
        <!-- <div class="col-6 mx-auto"> -->
        <!-- <h1>{{ title }}</h1> -->
        {% block content %}
        <!-- Replace this value -->
        {% endblock %}
        <!-- </div> -->
        <!-- </div> -->
    </div>
    {% include 'js.html' %}
</body>

</html>

 

home.html>

{% extends "base.html" %}

{% block content %}

<h1>{{ title }}</h1>

{% for a in blog_list %}
{% include 'blog/list-inline.html' with blog_post=a %}
{% endfor %}

{% endblock %}

 

viws>

from django.http import HttpResponse
from django.shortcuts import render
from django.template.loader import get_template

from .forms import ContactForm
from blog.models import BlogPost

# Model View Template (MVT)
# Dont Repeat Yourself = DRY
def home_page(request):
    my_title = "Hello there ..."
    qs = BlogPost.objects.all()[:5]
    context = {"title": "Welcome to Try Dajngo", "blog_list": qs}
    # if request.user.is_authenticated:
    # context = {"title": my_title, "my_list": [1, 2, 3, 4, 5]}
    # 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):
    # print(request.POST)
    form = ContactForm(request.POST or None)
    if form.is_valid():
        print(form.cleaned_data)
        form = ContactForm()
    context = {"title": "Contact Us", "form": form}
    return render(request, "form.html", context)


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]Model Managers and Custom QuerySets  (0) 2020.06.21
[Django]Publish Date, Timestamp & Updated  (0) 2020.06.21
[Django]Include the Navbar  (0) 2020.06.21
[Django]Blog Post Navigation  (0) 2020.06.21
[Django]Delete and Confirm  (0) 2020.06.21