[Python] I need to create a button in html

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Field and a button that changes a record in the Database when clicked. I want to not have this field, and when I press the button, I go to the already filled field, and I can customize it, and then save it by pressing the button.

    Here is my code

    today.html


    <title>Todoist</title>
    <h1>Сегодня</h1>
    <h2>Задачи</h2>
    <form method="POST" action="{% url 'todoist:add' %}" >{% csrf_token %}
    <input type="text" name='tasks' pattern=".{2,}" required title='Минимум два символа!!!'>
    <button type="submit" class="save btn btn-default">Добавить задачу</button>
    </form>


    {% for todo in todos%}
    <div>
    <p> {{todo.tasks}}</p>
    <a href={% url 'todoist:delete' today_id=todo.id%}>Удалить</a>
    <form action="{% url 'todoist:update' today_id=todo.id%}" method="post"> {% csrf_token %}
    <input type="text" name='tasks'>
    <button type="submit" class="save btn btn-default">Изменить</button>
    </form>
    </div>
    {% endfor %}

    views.py

    from django.shortcuts import render,redirect
    from .models import Today
    from django.views.decorators.http import require_http_methods


    def index(request):
    todos = Today.objects.all()
    return render(request, 'today.html', {'todos':todos})

    @require_http_methods(['POST'])
    def add(request):
    tasks = request.POST["tasks"]
    task = Today(tasks=tasks)
    task.save()
    return redirect('todoist:index')

    def delete(request, today_id):
    todo = Today.objects.get(id=today_id)
    todo.delete()
    return redirect('todoist:index')

    def update(request, today_id):
    todo = Today.objects.get(id=today_id)
    todo.tasks = request.POST.get('tasks')
    todo.save()
    return redirect('todoist:index')

    Continue reading...

Compartilhe esta Página