url 별칭
📌 url 별칭
① name
(urls.py)
from django.urls import path
from . import views
urlpatterns = [
path('index/', views.index, name="index"),
path('detail/<bpk>', views.detail, name="detail")
]
② {% url 'name' 넘겨줘야할 값(생략 가능) %}
(index.html)
<table border="1">
<tr>
<th>NO</th>
<th>SUBJECT</th>
<th>WRITER</th>
<th>HIT</th>
</tr>
{% for i in bset %}
<tr>
<td>{{ forloop.counter }}</td>
<td><a href="{% url 'detail' i.id %}">{{ i.subject }}</a></td> // 별칭 사용. 넘겨줘야 할 것은 옆에 명시.
<td>{{ i.writer }}</td>
<td>{{ i.hit }}</td>
</tr>
{% empty %}
<tr>
<th colspan="4">NO DATA</th>
</tr>
{% endfor %}
</table>
💡 name과 html에 사용한 ' '안 문자가 일치해야함.
'Django' 카테고리의 다른 글
[Django] render와 redirect (0) | 2022.12.19 |
---|---|
[Django] DB 가져와서 CRUD(4) - 상세 페이지 만들기(R) (0) | 2022.12.19 |
[Django] <var> (0) | 2022.12.19 |
[Django] DB 가져와서 CRUD(3) - DB 읽고 화면에 출력하기(R) (0) | 2022.12.19 |
[Django] DB 가져와서 CRUD(2) - DB 생성 및 레코드 추가 (0) | 2022.12.19 |
댓글