티스토리 뷰

Web/Django

[Django]base.html_최종

HAN_PY 2020. 8. 14. 22:02
반응형

base 분리

기본 설정을 하자.

hanpy로 프로젝트 생성했다.

articles로 app 생성했다.

base.html을 만든 templates위치를 아래 그림으로 확인하자.

base.html은 bootstrap CDN을 포함 시키자.

 

 

 


 

 

 

프로젝트와 앱 생성

 

 

 

setting.py 설정

 

 

 

대략 이런식이다.

 

 

 

 

부트스트렙적용

https://getbootstrap.com/docs/4.4/getting-started/introduction/

 

Introduction

Get started with Bootstrap, the world’s most popular framework for building responsive, mobile-first sites, with BootstrapCDN and a template starter page.

getbootstrap.com

CDN은 위의 페이지에 들어가서 붙여넣으면 된다.

 

 

 

 


최종 base.html

사용자인증 관리 후의 base.html   (전체 정리 하고 다시 보면 된다.)

 

<body>

    {% if user.is_authenticated %}

        <p>{{ user.username }}, 님 환영합니다.</p>

        <a href="{% url 'accounts:logout' %}">로그아웃</a>

    {% else %}

        <a href="{% url 'accounts:login' %}">로그인</a>

        <a href="{% url 'accounts:signup' %}">회원가입</a>

    {% endif %}

    <div class="container">

        {% block body %}

        {% endblock %}

    </div>

</body>

 

 

위와 같이 적으면 User가 짬뽁으로 된다.

  • base.html의 user는 settings.py에서 정의 한 내부값이다.
  • detail.html은 context에서 넘어온 user가 우선 적용 된다.

이러한 두가지 이유 때문에 base.html에서 detail.html로 넘어온 user도 로그인한 유저가 아닌, detail의 user의 id로 적용된다. 이러한 분제점을 해결하기 위해서는 base.html의 "user"를 "request.user"로 변경한다.

 

따라서 base.html은

<body>

    {% if request.user.is_authenticated %}

        <p>{{ request.user.username }}, 님 환영합니다.</p>

        <a href="{% url 'accounts:logout' %}">로그아웃</a>

    {% else %}

        <a href="{% url 'accounts:login' %}">로그인</a>

        <a href="{% url 'accounts:signup' %}">회원가입</a>

    {% endif %}

    <div class="container">

        {% block body %}

        {% endblock %}

    </div>

</body>

이런식으로 바꿔주면 된다.

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함