【django】todoリストアプリを作ってみる③(その他のシステム実装)【pythonメモ】

pythonLOGO

見た目を整えるCreateView

templatesフォルダにcreate.htmlを作成し、urls.pyとつなぎこむ

from django.urls import path
from .views import TodoList, TodoDetail, TodoCreate

urlpatterns = [
    path('list/', TodoList.as_view()), 
    path('detail/<int:pk>', TodoDetail.as_view()),
    path('create/', TodoCreate.as_view()),
]
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import TodoModel

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
{% extends 'base.html' %}

{% block content %}
<form action="" method="POST">
  {{form.as_p}}
  <input type="submit" value="作成する">
</form>

{% endblock content %}

{{form.as_p}}はdjangoタグで、pタグで囲ったものを出すという意味

ここでいったん表示を確認

python3 manage.py runserver
http://127.0.0.1:8000/create/

TodoCreateを使うにはfieldsが必要ですよ。と怒られる。

だからmodels.pyのどこを使うか指定する。

from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import TodoModel

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
{% extends 'base.html' %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
  {{form.as_p}}
  <input type="submit" value="作成する">
</form>

{% endblock content %}

ここでもう一度起動する

python3 manage.py runserver

で、これを入力しようとすると

http://127.0.0.1:8000/create/

またエラーが出ます

入力した後の画面が指定されていないためのエラー

get_absolute_url メソッドを使うかurlを指定するように指示されているので

from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import TodoModel
from django.urls import reverse_lazy

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
  success_url = reverse_lazy('')

succsess_url は入力が成功したもの

reverse_layzy はdjangoの遷移を戻すというメソッド

urls.py

djangoの特徴として、urlごとに表示(views,py)を見に行くが、リストに名前をつけることが可能

from django.urls import path
from .views import TodoList, TodoDetail, TodoCreate

urlpatterns = [
    path('list/', TodoList.as_view(), name='list'), 
    path('detail/<int:pk>', TodoDetail.as_view(), name='detail'),
    path('create/', TodoCreate.as_view(), name='create'),
]

こんな感じで

で、views.pyに名前として渡す

sfrom django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import TodoModel
from django.urls import reverse_lazy

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
  success_url = reverse_lazy('list')

サーバーを動かして確認する

http://127.0.0.1:8000/create/

作成するを押して見ると

http://127.0.0.1:8000/list/

無事に/listに戻ることが出来ました。

データを削除するときのテンプレートDeleteView

templatesフォルダにdelete.htmlを作成し、urls.pyとつなぎこむ

from django.urls import path
from .views import TodoList, TodoDetail, TodoCreate, TodoDelete

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'),
]
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, DeleteView
from .models import TodoModel
from django.urls import reverse_lazy

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
  success_url = reverse_lazy('list')

class TodoDelete(DeleteView):
  template_name = 'delete.html'
  model = TodoModel
  success_url = reverse_lazy('list')
{% extends 'base.html' %}

{% block content %}
<form action='' method="POST">{% csrf_token %}
  <input type="submit" value='削除します'>
</form>
{% endblock content %}

データを更新するときのテンプレートDeleteView

templatesフォルダにupdate.htmlを作成し、urls.pyとつなぎこむ

from django.urls import path
from .views import TodoList, TodoDetail, TodoCreate, TodoDelete, TodoUpdate

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'),
]
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, DeleteView, UpdateView
from .models import TodoModel
from django.urls import reverse_lazy

# Create your views here.

class TodoList(ListView):
  template_name = 'list.html'
  model = TodoModel

class TodoDetail(DetailView):
  template_name = 'detail.html'
  model = TodoModel

class TodoCreate(CreateView):
  template_name = 'create.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
  success_url = reverse_lazy('list')

class TodoDelete(DeleteView):
  template_name = 'delete.html'
  model = TodoModel
  success_url = reverse_lazy('list')

class TodoUpdate(UpdateView):
  template_name = 'update.html'
  model = TodoModel
  fields = ('title', 'memo', 'priority', 'duedate')
  success_url = reverse_lazy('list')
% extends 'base.html'%}

{% block content %}
<form action='' method='POST'>{% csrf_token %}
{{ form.title }}
{{ form.memo }}
{{ form.priority }}
{{ form.duedate }}
<input type="submit" value='更新する'>
</form>
{% endblock content %}


 

あ、宜しければ・・・。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です