@Radiss

Почему в todo-list дублируется вывод данных?

di-YCZ8.jpg

init_db.py

spoiler

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from db import Base, TodoItem

engine = create_engine("sqlite:///tasks.db", echo=True)
Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
s = Session()


for desc in ("1 задача", "2 задача",
"3 задача", "4 задача"):
    t = TodoItem(desc)
    s.add(t)

s.commit()



script.js

spoiler

$(document).ready(function ($) {
        $(document).on('click', '.checkbox', function(){
                $(this).parent().addClass('completed');
                $(this).attr('disabled', true);

                uid = $(this).attr('data-uid');
                $.get("/api/complete/" + uid);
        });

        $(document).on('click', '.remove', function(){
                $(this).parent().remove();
        });
});



index.tpl
spoiler

<html>
  <head>
    <title>Задачи на день</title>
    <link rel="stylesheet" href="static/styles.css">

    <!--<script src="http://code.jquery.com/jquery-3.3.1.slim.min.js"></script>-->
        <script
  src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
  integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
  crossorigin="anonymous"></script>
    <script src="static/script.js"></script>
  </head>
  <body>
  <div class="container">
    <h1>Текущие задачи</h1>
    <ul id="todo-list">
    % for task in tasks:
      % if task.is_completed:
      <li class="completed">
        <input class='checkbox' data-uid={{ task.uid }} type='checkbox'
         disabled='disabled' checked='checked' />
      % else:
      <li>
        <input class='checkbox' data-uid={{ task.uid }} type='checkbox' />       {{ task }}
      % end
        {{ task }}
        <a class="remove" href="/api/delete/{{ task.uid }}">X</a>
        <hr/>
      </li>
    % end
    </ul>
    <!--<form id="todo-add">
      <input type="text" id="new-todo-description" class="form-control"/>
      <button class="add" type="submit">+</button>
    </form>-->
       <form action="/add-task" method="post">
      <input type="text" name="description"/>
      <button type="submit">+</button>
       </form>
  </div>
  </body>

</html>



server.py

spoiler

from bottle import route, run, static_file, view, redirect, request
from db import TodoItem
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine("sqlite:///tasks.db")
Session = sessionmaker(bind=engine)
s = Session()


@route("/static/<filename:path>")
def send_static(filename):
    return static_file(filename, root="static")


@route("/")
@view("index")
def index():
    tasks = s.query(TodoItem).order_by(TodoItem.uid)
    return {"tasks": tasks}


@route("/add-task", method="POST")
def add_task():
    desc = request.POST.description.strip()
    if len(desc) > 0:
        t = TodoItem(desc)
        s.add(t)
        s.commit()
    return redirect("/")


@route("/api/delete/<uid:int>")
def api_delete(uid):
    s.query(TodoItem).filter(TodoItem.uid == uid).delete()
    s.commit()
    return redirect("/")


@route("/api/complete/<uid:int>")
def api_complete(uid):
    t = s.query(TodoItem).filter(TodoItem.uid == uid).first()
    t.is_completed = True
    s.commit()
    return "Ok"

###
run(host="localhost", port=8080)



db.py

spoiler

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Boolean

Base = declarative_base()


class TodoItem(Base):
    __tablename__ = "tasks"

    uid = Column(Integer, primary_key=True)
    description = Column(String(255))
    is_completed = Column(Boolean, default=False)

    def __init__(self, description):
        self.description = description

    def __str__(self):

        return self.description.lower()

  • Вопрос задан
  • 77 просмотров
Решения вопроса 1
Seasle
@Seasle Куратор тега JavaScript
Помог - отметь решением \( ゚ヮ゚)/

% else:
  <li>
    <input class='checkbox' data-uid={{ task.uid }} type='checkbox' />       {{ task }}
% end
  {{ task }}


Видимо поэтому.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы