[PYTHON] Create a Todo app with Django ⑤ Create a task editing function

Next, we will create a task editing function.

List of articles

Create a Todo app with Django ① Build an environment with Docker Create a Todo app with Django ② Create a folder list page Create a Todo app with Django ③ Create a task list page Create Todo app with Django ④ Implementation of folder and task creation function Create a Todo app with Django ⑤ Create a task editing function

URL settings

First, set the URL. Add the following sentence to todo / urls.py.

todo/urls.py


path('<int:id>/tasks/<int:task_id>', views.edit_task, name='tasks.edit')

Insert link

Insert the link in the edit part of templates / index.html as shown below.

templates/index.html


<a href="{% url 'tasks.edit' id=current_folder_id task_id=task.id %}}">Edit</a>

template

Create ʻedit.html under the templates directory. Then edit ʻedit.html as follows.

templates/edit.html


{% extends 'base.html' %}

{% block styles %}
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
  <link rel="stylesheet" href="https://npmcdn.com/flatpickr/dist/themes/material_blue.css">
{% endblock %}

{% block content %}
  <div class="container">
    <div class="row">
      <div class="col col-md-offset-3 col-md-6">
        <nav class="panel panel-default">
          <div class="panel-heading">Edit task</div>
          <div class="panel-body">
            <form method="POST">
              {% csrf_token %}
              {{ form.as_p }}
              <div class="text-right">
                <button type="submit" class="btn btn-primary">Send</button>
              </div>
            </form>
          </div>
        </nav>
      </div>
    </div>
  </div>
{% endblock %}

{% block scripts %}
  <script src="https://npmcdn.com/flatpickr/dist/flatpickr.min.js"></script>
  <script src="https://npmcdn.com/flatpickr/dist/l10n/ja.js"></script>
  <script>
    flatpickr(document.getElementsByName('due_date'), {
      locale: 'ja',
      minDate: new Date()
    });
  </script>
{% endblock %}

Next, I will write a view.

View Add the following ʻedit_task` method to the view.

views.py


def edit_task(request, id, task_id):
    #Get the selected task
    task = get_object_or_404(Task, id=task_id)
    if request.method == "POST":
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            task = form.save(commit=False)
            task.save()
            return redirect('tasks.index', id=task.folder_id.id)
    else:
        form = TaskForm(instance=task)
    return render(request, 'edit.html', {'form': form}, {'task':task})

When data is added to request.POST, the processing of the if statement is written so that the contents entered in the form are saved in the database.

Complete!

This is the end of this chapter and the Todo app is complete! The code so far can be found in the repository's chapter5 branch.

From here, I think you can also add a delete function or an authentication function, so I think it's a good idea to customize it! Thank you for referring to this tutorial!

List of articles

Create a Todo app with Django ① Build an environment with Docker Create a Todo app with Django ② Create a folder list page Create a Todo app with Django ③ Create a task list page Create Todo app with Django ④ Implementation of folder and task creation function Create a Todo app with Django ⑤ Create a task editing function

Recommended Posts

Create a Todo app with Django ⑤ Create a task editing function
Create a Todo app with Django REST Framework + Angular
Create a Todo app with Django ④ Implement folder and task creation functions
Create a Todo app with Django ① Build an environment with Docker
Create a homepage with django
Create a file uploader with Django
Create a Python function decorator with Class
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Create a GUI app with Python's Tkinter
Create a simple web app with flask
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
Create a dashboard for Network devices with Django!
How to create a multi-platform app with kivy
Create a one-file hello world application with django
A simple to-do list created with Python + Django
Until you create a new app in Django
Create a Django schedule
Create Django Todo list
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
I tried to create a table only with Django
Create a native GUI app with Py2app and Tkinter
[Practice] Make a Watson app with Python! # 2 [Translation function]
Deploy a Django app made with PTVS on Azure
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Web App Development Practice: Create a Shift Creation Page with Django! (Write a base template)
Web App Development Practice: Create a Shift Creation Page with Django! (Authentication system processing)
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
[Go language] Create a TUI app with Elm Architecture Create a slightly rich ToDo app with bubble tea
Todo app with django-bootstrap-modal-forms plugin
Create a function in Python
Create an API with Django
Create ToDo List [Python Django]
Create a heatmap with pyqtgraph
Create a directory with python
Create a shogi game record management app using Django 4 ~ Create View ~
Create a machine learning app with ABEJA Platform + LINE Bot
Build a bulletin board app from scratch with Django. (Part 2)
Build a bulletin board app from scratch with Django. (Part 3)
Create a web API that can deliver images with Django
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Web App Development Practice: Create a Shift Creation Page with Django! (Design of database model)
Steps to create a Django project
Create a shogi game record management app using Django 2-Database settings-
Create a temporary file with django as a zip file and return it
Create a shogi game record management app using Django 6 ~ Template division ~
Create a virtual environment with Python!
Rails users try to create a simple blog engine with Django
Create and deploy a Django (PTVS) app using Azure Table storage
Django Tips-Create a ranking site with Django-
Implement a Django app on Hy
Creating a simple app with flask
Build a web application with Django
Make a scraping app with Python + Django + AWS and change jobs
Make a filter with a django template
Create a video player with PySimpleGUI + OpenCV 3 Addition of mask function
Create a poisson stepper with numpy.random
Create a REST API to operate dynamodb with the Django REST Framework
Create a LINE Bot in Django