|
371 | 371 | content = form.content.data,
|
372 | 372 | types = form.types.data,
|
373 | 373 | image = form.image.data,
|
374 |
| - created_at = datetime.now() |
| 374 | + created_at = datetime.now(), |
| 375 | + is_valid = True |
375 | 376 | )
|
376 | 377 | # 保存数据
|
377 | 378 | db.session.add(new_obj)
|
|
382 | 383 |
|
383 | 384 | ### 🔍 实现增删改查
|
384 | 385 |
|
| 386 | ++ 增 |
| 387 | + |
| 388 | + `app.py` |
| 389 | + |
| 390 | + ```python |
| 391 | + @app.route('/admin/add/', methods=('GET', 'POST')) |
| 392 | + def add(): |
| 393 | + # 新闻后台数据新增 |
| 394 | + form = NewsForm() |
| 395 | + if form.validate_on_submit(): |
| 396 | + # 获取数据 |
| 397 | + new_obj = News( |
| 398 | + title = form.title.data, |
| 399 | + content = form.content.data, |
| 400 | + types = form.types.data, |
| 401 | + image = form.image.data, |
| 402 | + created_at = datetime.now(), |
| 403 | + is_valid = True |
| 404 | + ) |
| 405 | + # 保存数据 |
| 406 | + db.session.add(new_obj) |
| 407 | + db.session.commit() |
| 408 | + return redirect(url_for('admin')) |
| 409 | + return render_template('admin/add.html', form=form) |
| 410 | + ``` |
| 411 | + |
| 412 | + `add.html` |
| 413 | + |
| 414 | + ```html |
| 415 | + <form class="form-horizontal" role="form" method="post"> |
| 416 | + <div class="form-group"> |
| 417 | + <label for="inputEmail3" class="col-sm-2 control-label"> |
| 418 | + {{ form.title.label.text }} |
| 419 | + </label> |
| 420 | + <div class="col-sm-10"> |
| 421 | + {{ form.title }} |
| 422 | + </div> |
| 423 | + </div> |
| 424 | + |
| 425 | + <div class="form-group"> |
| 426 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 427 | + {{ form.content.label.text }} |
| 428 | + </label> |
| 429 | + <div class="col-sm-10"> |
| 430 | + {{ form.content }} |
| 431 | + </div> |
| 432 | + </div> |
| 433 | + |
| 434 | + <div class="form-group"> |
| 435 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 436 | + {{ form.types.label.text }} |
| 437 | + </label> |
| 438 | + <div class="col-sm-10"> |
| 439 | + {{ form.types }} |
| 440 | + </div> |
| 441 | + </div> |
| 442 | + |
| 443 | + <div class="form-group"> |
| 444 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 445 | + {{ form.image.label.text }} |
| 446 | + </label> |
| 447 | + <div class="col-sm-10"> |
| 448 | + {{ form.image }} |
| 449 | + </div> |
| 450 | + </div> |
| 451 | + |
| 452 | + <div class="form-group"> |
| 453 | + <div class="col-sm-offset-2 col-sm-10"> |
| 454 | + {{ form.csrf_token }} |
| 455 | + {{ form.submit }} |
| 456 | + </div> |
| 457 | + </div> |
| 458 | + </form> |
| 459 | + ``` |
| 460 | + |
| 461 | ++ 删 |
| 462 | + |
| 463 | + `app.py` |
| 464 | + |
| 465 | + ```python |
| 466 | + @app.route('/admin/delete/<int:pk>/', methods=('GET', 'POST')) |
| 467 | + def delete(pk): |
| 468 | + # 新闻后台数据删除 |
| 469 | + news_obj = News.query.get(pk) |
| 470 | + if not news_obj: |
| 471 | + return 'no' |
| 472 | + news_obj.is_valid = False |
| 473 | + db.session.add(news_obj) |
| 474 | + db.session.commit() |
| 475 | + return 'yes' |
| 476 | + ``` |
| 477 | + |
| 478 | + `index.html` |
| 479 | + |
| 480 | + ```html |
| 481 | + <a class="btn btn-danger" href="javascript:;" data-url="{{ url_for('delete', pk=news_obj.id) }}">删除</a> |
| 482 | + |
| 483 | + <script> |
| 484 | + $(function() { |
| 485 | + $('.btn-danger').on('click', function() { |
| 486 | + var btn = $(this); |
| 487 | + if(confirm('确定删除该记录吗?')) { |
| 488 | + $.post(btn.attr('data-url'), function(data) { |
| 489 | + if(data === 'yes') { |
| 490 | + btn.parents('tr').hide(); |
| 491 | + } else { |
| 492 | + alert('删除失败'); |
| 493 | + } |
| 494 | + }) |
| 495 | + } |
| 496 | + }) |
| 497 | + }) |
| 498 | + </script> |
| 499 | + ``` |
| 500 | + |
| 501 | ++ 改 |
| 502 | + |
| 503 | + `app.py` |
| 504 | + |
| 505 | + ```python |
| 506 | + @app.route('/admin/update/<int:pk>/', methods=('GET', 'POST')) |
| 507 | + def update(pk): |
| 508 | + # 新闻后台数据修改 |
| 509 | + news_obj = News.query.get(pk) |
| 510 | + # 如果没有数据,则返回 |
| 511 | + if not news_obj: |
| 512 | + return redirect(url_for('admin')) |
| 513 | + form = NewsForm(obj=news_obj) |
| 514 | + if form.validate_on_submit(): |
| 515 | + # 获取数据 |
| 516 | + news_obj.title = form.title.data |
| 517 | + news_obj.content = form.content.data |
| 518 | + # 保存数据 |
| 519 | + db.session.add(news_obj) |
| 520 | + db.session.commit() |
| 521 | + return redirect(url_for('admin')) |
| 522 | + return render_template('admin/update.html', form=form) |
| 523 | + ``` |
| 524 | + |
| 525 | + `index.html` |
| 526 | + |
| 527 | + ```html |
| 528 | + <a class="btn btn-info" href="{{ url_for('update', pk=news_obj.id) }}">修改</a> |
| 529 | + ``` |
| 530 | + |
| 531 | + `update.html` |
| 532 | + |
| 533 | + ```html |
| 534 | + <form class="form-horizontal" role="form" method="post"> |
| 535 | + <div class="form-group"> |
| 536 | + <label for="inputEmail3" class="col-sm-2 control-label"> |
| 537 | + {{ form.title.label.text }} |
| 538 | + </label> |
| 539 | + <div class="col-sm-10"> |
| 540 | + {{ form.title }} |
| 541 | + </div> |
| 542 | + </div> |
| 543 | + |
| 544 | + <div class="form-group"> |
| 545 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 546 | + {{ form.content.label.text }} |
| 547 | + </label> |
| 548 | + <div class="col-sm-10"> |
| 549 | + {{ form.content }} |
| 550 | + </div> |
| 551 | + </div> |
| 552 | + |
| 553 | + <div class="form-group"> |
| 554 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 555 | + {{ form.types.label.text }} |
| 556 | + </label> |
| 557 | + <div class="col-sm-10"> |
| 558 | + {{ form.types }} |
| 559 | + </div> |
| 560 | + </div> |
| 561 | + |
| 562 | + <div class="form-group"> |
| 563 | + <label for="inputPassword3" class="col-sm-2 control-label"> |
| 564 | + {{ form.image.label.text }} |
| 565 | + </label> |
| 566 | + <div class="col-sm-10"> |
| 567 | + {{ form.image }} |
| 568 | + </div> |
| 569 | + </div> |
| 570 | + |
| 571 | + <div class="form-group"> |
| 572 | + <div class="col-sm-offset-2 col-sm-10"> |
| 573 | + {{ form.csrf_token }} |
| 574 | + {{ form.submit }} |
| 575 | + </div> |
| 576 | + </div> |
| 577 | + </form> |
| 578 | + ``` |
| 579 | + |
| 580 | ++ 查 |
| 581 | + |
| 582 | + `app.py` |
| 583 | + |
| 584 | + ```python |
| 585 | + @app.route('/admin/') |
| 586 | + @app.route('/admin/<int:page>/') |
| 587 | + def admin(page=None): |
| 588 | + # 新闻后台管理首页 |
| 589 | + if page is None: |
| 590 | + page = 1 |
| 591 | + news_list = News.query.filter_by(is_valid=True).paginate(page=page, per_page=5) |
| 592 | + return render_template('admin/index.html', news_list=news_list) |
| 593 | + ``` |
| 594 | + |
| 595 | + `index.html` |
| 596 | + |
| 597 | + ```html |
| 598 | + <table class="table table-striped"> |
| 599 | + <thead> |
| 600 | + <tr> |
| 601 | + <th>编号</th> |
| 602 | + <th>新闻标题</th> |
| 603 | + <th>类别</th> |
| 604 | + <th>添加时间</th> |
| 605 | + <th>操作</th> |
| 606 | + </tr> |
| 607 | + </thead> |
| 608 | + <tbody> |
| 609 | + {% for news_obj in news_list.items %} |
| 610 | + <tr> |
| 611 | + <td>{{ news_obj.id }}</td> |
| 612 | + <td>{{ news_obj.title }}</td> |
| 613 | + <td>{{ news_obj.types }}</td> |
| 614 | + <td>{{ news_obj.created_at }}</td> |
| 615 | + <td> |
| 616 | + <a class="btn btn-info" href="{{ url_for('update', pk=news_obj.id) }}">修改</a> |
| 617 | + <a class="btn btn-danger" href="javascript:;" data-url="{{ url_for('delete', pk=news_obj.id) }}">删除</a> |
| 618 | + </td> |
| 619 | + </tr> |
| 620 | + {% endfor %} |
| 621 | + </tbody> |
| 622 | + </table> |
| 623 | + ``` |
| 624 | + |
385 | 625 | **[⤴ get to top](#flask-教程)**
|
0 commit comments