[PYTHON] Create Django Todo list

Added the function to manage the Todo list for each facility! I was thinking of adjusting the input screen for shifts and creating a validation rule function, but it was quite difficult and I couldn't find it, so I made a new app (laughs).

The screen looks like this. image.png

For the display of the completed part, I considered switching with the check box and asked a question with Qiita, but since I do not understand javascript, I implemented it with the a tag based on the hint I received.

image.png

It's a detail screen, but if it's a basic function of Django, it's really too simple and it's not tasteful (laugh)

I made an app while thinking that I should be able to add input assistance and input check function here (laugh)

todo/views.py



from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, DeleteView, UpdateView
from .models import TodoModel
from shisetsu.models import *
from accounts.models import *
from django.urls import reverse_lazy

# Create your views here.

def todolistfunc(request):
    #Get affiliation information from logged-in user
    current_user = request.user
    check_value =  request.POST.getlist('check')
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user).values_list("shisetsu_name", flat=True)
    UserShozoku_list = list(UserShozoku_list)
    if 'all' in request.GET:
        todo_list = TodoModel.objects.filter(shisetsu_name__in = UserShozoku_list).select_related().all().order_by("plants_enddate")
        kako = "off" 
    else:
        todo_list = TodoModel.objects.filter(shisetsu_name__in = UserShozoku_list).filter(enddate__isnull=True).select_related().all().order_by("plants_enddate")
        kako = "on"

    shisetsu_object = Shisetsu.objects.all()
    user_list = User.objects.select_related().all().order_by("profile__hyoujijyun")
    #Reacquired for facility button display
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user)

    context = {
        'todo_list': todo_list,
        'shisetsu_object': shisetsu_object,
        'user_list': user_list,
        'UserShozoku_list':UserShozoku_list,
        'kako':kako,
    }
    return render(request,'todo/list.html', context)

def todolistfilterfunc(request, shisetsu_num):
    shisetsu_num = int(shisetsu_num)
    #Get affiliation information from logged-in user
    current_user = request.user
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user).values_list("shisetsu_name", flat=True)
    UserShozoku_list = list(UserShozoku_list)
    #todo_list = TodoModel.objects.filter(shisetsu_name__in = UserShozoku_list).select_related().all().order_by("plants_enddate")
    if 'all' in request.GET:
        todo_list = TodoModel.objects.filter(shisetsu_name = shisetsu_num).select_related().all().order_by("plants_enddate")
        kako = "off" 
    else:
        todo_list = TodoModel.objects.filter(shisetsu_name = shisetsu_num).filter(enddate__isnull=True).select_related().all().order_by("plants_enddate")
        kako = "on"
        
    shisetsu_object = Shisetsu.objects.filter(id__in = UserShozoku_list)
    print(shisetsu_object)
    user_list = User.objects.select_related().all().order_by("profile__hyoujijyun")

    #Reacquired for facility button display
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user)

    context = {
        'todo_list': todo_list,
        'shisetsu_object': shisetsu_object,
        'user_list': user_list,
        'UserShozoku_list':UserShozoku_list,
    }
    return render(request,'todo/list.html', context)

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

class TodoCreate(CreateView):
    template_name = 'todo/create.html'
    model = TodoModel
    fields = ('title', 'memo', 'priority','shisetsu_name', 'user', 'plants_startdate', 'plants_enddate', 'enddate')
    success_url = reverse_lazy('todo:list')

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

class TodoUpdate(UpdateView):
    template_name = 'todo/update.html'
    model = TodoModel
    fields = ('title', 'memo', 'priority','shisetsu_name', 'user', 'plants_startdate', 'plants_enddate', 'enddate')
    success_url = reverse_lazy('todo:list')

urls.py


from django.urls import path, include
from .views import todolistfunc, todolistfilterfunc, TodoDetail,TodoCreate, TodoDelete, TodoUpdate

app_name = 'todo'
urlpatterns = [
    path('listfilter/<int:shisetsu_num>', todolistfilterfunc, name='listfilter'),
    path('list/', todolistfunc, 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'),
]

todo/list.html


{% extends 'templates/accounts/base.html' %}
{% load static %}
{% block header %}
<form action="" method="POST">{% csrf_token %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-4">Todo list</h1>
      <p class="lead">Do what you can!</p>
      <a href="{% url 'todo:create' %}" class="btn-info btn active">Create New</a>
      <a href="{% url 'todo:list' %}" button type="button" class="btn btn-outline-dark">all</a>
      {% for shisetsu in shisetsu_object %}
          {% for UserShozoku in UserShozoku_list %}
              {% if shisetsu.name|stringformat:"s" == UserShozoku.shisetsu_name|stringformat:"s" %}
                  <a href="{% url 'todo:listfilter' shisetsu.pk %}" button type="submit" class="btn btn-outline-dark" span style="background-color:{{ shisetsu.color }}">{{ shisetsu.name }}</span></a>
              {% endif %}
          {% endfor %}
      {% endfor %}
Including completion
      <a href="{% url 'todo:list' %}?all" button type="button" class="btn btn-outline-dark">all</a>
      {% for shisetsu in shisetsu_object %}
          {% for UserShozoku in UserShozoku_list %}
              {% if shisetsu.name|stringformat:"s" == UserShozoku.shisetsu_name|stringformat:"s" %}
                  <a href="{% url 'todo:listfilter' shisetsu.pk %}?all" button type="submit" class="btn btn-outline-dark" span style="background-color:{{ shisetsu.color }}">{{ shisetsu.name }}</span></a>
              {% endif %}
          {% endfor %}
      {% endfor %}
      </div>
    </div>
  </div>
</form>

{% endblock header %}

{% block content %}

<table class="table table-striped table-bordered table-hover ">
      <thead>
        <th>Name of facility</th>
        <th>title</th>
        <th>priority</th>
        <th>Person in charge</th>
        <th>Scheduled start date</th>
        <th>Expected end date</th>
      </thead>
      <tr>
        {% for todo in todo_list %}
            <tbody class="table-{{ todo.priority }}">
                <td >{{ todo.shisetsu_name|default:"" }}</td>
                <td ><a href="{% url 'todo:update' todo.id %}">{{ todo.title }}</a></td>
                <td >{{ todo.get_priority_display }} </td>
                <td >{{ todo.user.last_name|default:"" }} {{ todo.user.first_name|default:"" }}</td>
                <td >{{ todo.plants_startdate|default:"" }} </td>
                <td >{{ todo.plants_enddate|default:"" }}</td>
            </tbody>
        {% endfor %}
        </tr>
      </div>
    </table>

{% endblock content %}

<script type="text/javascript">
  //Get checkbox from id
  const checkbox = documents.getElementsById('check');
  //Set a callback that will be called when the status of the checkbox changes
  checkbox.addEventListener('change', (e) => {
    //Separate movements in the state of checkbox
    if (checkbox.checked) {
      location.href = 'todo:list'?all'
    } else {
      location.href = 'todo:list'
    }
  });
</script>
``

The last javascript is what you answered. In the future, I would like to improve my skills so that I can implement what you have taught me.


Recommended Posts

Create Django Todo list
Create ToDo List [Python Django]
Create and list Django models
Create a Todo app with Django ③ Create a task list page
Create a Django schedule
A simple to-do list created with Python + Django
Create a Todo app with Django REST Framework + Angular
Create a Todo app with the Django REST framework
Create a Todo app with Django ⑤ Create a task editing function
Create an API with Django
Django beginners create simple apps 3
Django beginners create simple apps 1
Create a homepage with django
Shell to create django project
Django beginners create simple apps 2
Create a Django login screen
Create your own Django middleware
Django beginners create simple apps 5
Create a Todo app with Django ① Build an environment with Docker
Steps to create a Django project
Bottle Tutorial: TODO List Application Bug
Create new application use python, django
Django
[Django] Create your own 403, 404, 500 error pages
Create a file uploader with Django
Create a LINE Bot in Django
Create a Todo app with Django ④ Implement folder and task creation functions
Create RESTful APIs with Django Rest Framework
Create a model for your Django schedule
Create an update screen with Django Updateview
Create your first app with Django startproject
Create Page / Todo Block with Notion API
Implement the ability to reserve what happens regularly in your Django Todo list
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)