본문 바로가기

Python_WEB/Tweetme

[Django]Rapid Implement of Bootstrap Theme

CodingEntrepreneurs Django 강의 정리

navbar>

네비게이션바

https://getbootstrap.com/docs/4.5/components/navbar/

 

Navbar

Documentation and examples for Bootstrap’s powerful, responsive navigation header, the navbar. Includes support for branding, navigation, and more, including support for our collapse plugin.

getbootstrap.com

https://ko.wikipedia.org/wiki/%EB%82%B4%EB%B9%84%EA%B2%8C%EC%9D%B4%EC%85%98_%EB%B0%94

 

내비게이션 바 - 위키백과, 우리 모두의 백과사전

 

ko.wikipedia.org

 

!important>

나중에 설정한 값이 적용되지 않게 한다.

 

base.html>

<!-- templates/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">
    <style>
        .bg-tweetme {
            background-color: #8e1f1f !important;
        }
    </style>
    <title>Tweetme 2{% block head_title %}{% endblock head_title %}</title>
</head>

<body>
    {% include "components/navbar.html" %}
    <div class="container">
        {% block content %}
        {% endblock content %}
    </div>
    <!-- DRY Don't Repeat Yourself -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
        crossorigin="anonymous"></script>
</body>

</html>

 

navbar.html>

<-- templates/components/navbar.html -->

<nav class="navbar navbar-expand-lg navbar-dark bg-tweetme mb-4">
    <a class="navbar-brand" href="/">Tweetme</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
        aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse d-none" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto d-none">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0 d-none">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

 

home.html>

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

{% extends 'base.html' %}

{% block head_title %}
this is amazing!!!!
{% endblock head_title%}


{% block content %}
<div class="row text-center">
    <div class="col">
        <h1>Welcome to Tweetme 2</h1>
    </div>
</div>
<div class="row" id="tweets">
    Loading...
</div>

<script>
    const tweetsElement = document.getElementById("tweets") // get an html element
    // tweetsElement.innerHTML = 'Loading...' // set new html in that element
    // var el1 = "<h1>Hi there 1</h1>"
    // var el2 = "<h1>Hi there 2</h1>"
    // var el3 = "<h1>Hi there 3</h1>"
    // tweetsElement.innerHTML = el1 + el2 + el3

    const xhr = new XMLHttpRequest()
    const method = 'GET' // "POST"
    const url = "/tweets"
    const responseType = "json"

    function handleDidLike(tweet_id, currentCount) {
        console.log(tweet_id, currentCount)
        return
    }

    function LikeBtn(tweet) {
        return "<button class='btn btn-primary btn-sm' onclick=handleDidLike(" +
            tweet.id + "," + tweet.likes + ")>" + tweet.likes + "Likes</button>"
    }

    function formatTweetElement(tweet) {
        var formattedTweet = "<div class='col-12 col-md-10 mx-aut o border rounded py-3 mb-4 tweet' id='tweet-" + tweet.id
            + "'><p>" + tweet.content +
            "</p><div class='btn-group'>" + LikeBtn(tweet) +
            "</div></div>"
        return formattedTweet
    }
    xhr.responseType = responseType
    xhr.open(method, url)
    xhr.onload = function () {
        const serverResponse = xhr.response
        const listedItems = serverResponse.response // array
        var finalTweetStr = ""
        var i;
        for (i = 0; i < listedItems.length; i++) {
            var tweetObj = listedItems[i]
            var currentItem = formatTweetElement(tweetObj)
            finalTweetStr += currentItem
        }
        tweetsElement.innerHTML = finalTweetStr
        // console.log(listedItems)
    }
    xhr.send()


</script>
{% endblock content %}

 

결과물>

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

[Django]Tweet Create Form  (0) 2020.07.14
[Django]Rapid Implement of Bootstrap Theme  (0) 2020.07.14
[Django]Like Button Rendering  (0) 2020.07.08
[Django]Format Tweet Method  (0) 2020.07.08
[Django]Tweets to HTML via JavaScript  (0) 2020.07.08
[Django]Replace HTML Content with JavaScript  (0) 2020.07.08