[0614] TIL 46์ผ์ฐจ

nikevapormaxยท2022๋…„ 6์›” 14์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
48/116
post-custom-banner

๐Ÿ˜‚ Django Project

๐Ÿ˜ญ ํ”„๋กœ์ ํŠธ ๋ฐœํ‘œ ์ง„ํ–‰

KPT ํšŒ๊ณ 

๐Ÿ˜ญ ํ”„๋กœ์ ํŠธ ๋ฆฌํŒฉํ† ๋ง

  • ํ”„๋กœ์ ํŠธ์˜ ์ƒ๋‹จ ์žฅ๋ฅด ์นดํ…Œ๊ณ ๋ฆฌ๋ฐ”๋ฅผ ์ „๋‚  ์ง„ํ–‰ํ–ˆ์„ ๋•Œ POST ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ๋ฐ›์•„์„œ ๋ฐฑ์•ค๋“œ๋กœ ์ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“ค์—ˆ์—ˆ๋‹ค. ์ด๊ฒƒ์„ GET ๋ฐฉ์‹์œผ๋กœ ๋‹ค์‹œ ๋ฆฌํŒฉํ† ๋งํ•ด๋ณด์•˜๋‹ค.
  • ์‹ค์ œ๋กœ GET์œผ๋กœ ํ•˜๋‹ˆ a ๋งํฌ๋งŒ์œผ๋กœ๋„ ํ”„๋ก ํŠธ ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ๋˜ํ•œ 19๊ฐœ์˜ ์žฅ๋ฅด๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉ์ด ์•„๋‹Œ for ๋ฌธ์„ ํ™œ์šฉํ•ด ๊น”๋”ํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด ์ฃผ์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ for ๋ฌธ ์•ˆ์— ์“ฐ์ด๋Š” ๊ฐ’์„ ๋ฐฑ์•ค๋“œ์—์„œ ํ•œ ๋ฒˆ ๋” ๋ณด๋‚ด์ฃผ์–ด์•ผ ์ƒ๋‹จ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ๊ณ„์† ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • movie/views.py
# ํ™”๋ฉด ์ƒ๋‹จ tag๊ฐ’ ๋‚˜์˜ค๋Š” nav ๋ฐ”
def tagging(request, name):
    if request.method == 'GET':
        tag_all = Tag.objects.all()

        tag = Tag.objects.get(tag=name)
        max_score = tag.movies.filter(tag=tag.id)

        return render(request, 'movie/home.html', {'movies': max_score,'tag_all': tag_all, 'tag': tag})
  • movie/urls.py
from django.urls import path
from movie import views

urlpatterns = [
    path('', views.home, name='home'),
    path('detail/<int:id>/', views.detail, name='detail'),
    path('search/', views.search, name='search'),
    path('tag/<str:name>/', views.tagging, name='tagging'),
]
  • movie/home.html
{% block content %}
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" id="all" href="http://127.0.0.1:8000/">All</a>
        </li>
        {% for tag in tag_all %}
            <li class="nav-item">
                <a class="nav-link" id="{{ tag.id }}" href="/tag/{{ tag }}/">{{ tag }}</a>
            </li>
        {% endfor %}
    </ul>

<!-- ์ค‘๊ฐ„ ์ƒ๋žต -->

{% endblock %}

{% block script %}
    var aTags = document.getElementById('{{ tag.id }}')
    var tag = aTags.innerText

    var all = document.getElementById('all')
    
    var searchText = "{{ tag }}";

    if (tag == searchText) {
        aTags.classList.toggle('active')
        all.classList.toggle('active')
    } else {
        all.classList.toggle('active')
    }
{% endblock %}
profile
https://github.com/nikevapormax
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€