중급_문법

북마크. 즐겨찾기 앱으로 ... url , generic View

나도초딩 2023. 11. 5.

 

https://github.com/nodo1014/bookmark

 

GitHub - nodo1014/bookmark: 배프

배프. Contribute to nodo1014/bookmark development by creating an account on GitHub.

github.com

 

urls.py View

url 패턴에 이름 지정해서, 템플릿파일에서 하이퍼 링크로 사용하고, reverse(' url 패턴 이름 ' )

수정/삭제 후, 이동 페이지 지정하기==> success_url 또는 get_absolute_url(self)

1) view 에 success_url = reverse_lazy('list')

 

2) 모델 에 get_absolute_url(self) 메서드

reverse("url패턴이름", args=[str(self.id)]

 

 

 

디자인. 템플릿 확장 TEMPLATES  에 템플릿 디렉토리 추가

 

templates/base.html

 

 

 

1) 부트스트랩 4.6 적용

 

 

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

<!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://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

        <title>{% block title %}{% endblock %}</title>
   

    </head>
    <body>
        {% block content %}{% endblock %}

        <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
    

    </body>
</html>

 

2) 부트스트랩 5.3 적용

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <title>{% block title %}{% endblock %}</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

    </head>
    <body>
        {% block content %}{% endblock %}

        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>

    </body>
</html>

 

style.css 및 이미지 등 정적 파일은 static 디렉토리로

루트에 static 생성 후, settings.py 에 추가.

static/style.css

base.html 에 {% load static %}

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<link href="{% static 'style.css' %}" rel="stylesheet" >
<title>{% block title %}{% endblock %}</title>
</head>

<div class="alert alert-danger">{{object}} 를 삭제하시겠습니까?</div>
 

 

리스트 view 에 페이지네이션 추가

{% block pagination %}

    {% if is_paginated %}
        <ul class="pagination justify-content-center pagination-sm">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="{% url 'list' %}?page={{ page_obj.previous_page_number }}" tabindex="-1">page_obj.has_previous</a>
            </li>
            {% else %}
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">이전(page_obj.has_previous없음)</a>
            </li>
            {% endif %}
            
            {% for object in page_obj.paginator.page_range %}
            <li class="page-item" {% if page_obj.number == forloop.counter %}disabled{% endif %}>
                <a class="page-link" href="{{request.path}}?page={{ forloop.counter }}">{{forloop.counter}}</a>
            </li>
            {% endfor %}

            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="{% url 'list' %}?page={{ page_obj.next_page_number }}" tabindex="-1">page_obj.has_next</a>
            </li>
            {% else %}
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">page_obj.has_next(page_obj.has_next없음)</a>
            </li>
            {% endif %}
        </ul>
        <!-- 페이징처리 끝 -->
    {% endif %}
{% endblock %}

'중급_문법' 카테고리의 다른 글

파이썬 클래스  (0) 2023.04.09

댓글