0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DjangoでTodoアプリ作成その4urlの設定

0
Posted at

一覧画面のボタンにurlを設定する。

href="{% url 'detail' item.pk %}"では、urlというdjangoの機能を使用して'detail'画面に遷移することを指定している。'detail'はurls.pyのnameで指定している。item.pkはDBから取得してきたデータのオブジェクトリスト(object_list)のプライマリーキーのこと。

template/list.html
{%  extends 'base.html' %}
{% block header %}
<div class="container">
  <div class="bg-warning p-3 p-sm-5 my-4 rounded">
    <h1 class="display-4">Todolist</h1>
    <p class="lead">Todolistを作って毎日を効率的に過ごしましょう。</p>
</div>
{% endblock %}

{% block content %}
    <div class='container'>
    {% for item in object_list %}
        <div class="alert alert-{{ item.priority }}" role="alert">
        <p class="fs-1">{{ item.title }}</p>
        <a class="btn btn-info btn-sm" href="{% url 'update' item.pk %}" role="button">編集</a>
        <a class="btn btn-info btn-sm" href="{% url 'delete' item.pk %}" role="button">削除</a>
        <a class="btn btn-info btn-sm" href="{% url 'detail' item.pk %}" role="button">詳細</a>
        </div>
    {% endfor %}
    </div>
{% endblock %}
todo.urls.py
urlpatterns = [
    path('list/', TodoList.as_view(), name='list'),
    path('detail/<int:pk>', TodoDetail.as_view(), name='detail'),
    path('create/', TodoCreate.as_view(), name='create'),
    path('delete/<int:pk>', TodoDelete.as_view(), name='delete'),
    path('update/<int:pk>', TodoUpdate.as_view(), name='update'),
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?