The Seven Year Itch.

MENU

Flask get_or_404 和 Django get_object_or_404使用和区别

December 26, 2017 • Read: 326 • Python,Django,Flask

Flask使用:


@admin.route('/users/edituser/<int:id>', methods=["GET", "POST"])
def user_edit(id=None):
    """
    编辑用户
    Author: little、seven
    :param id:
    :return:
    """
    user = User.query.get_or_404(int(id))
    ......

Django使用:
首先导入包
from django.shortcuts import render, get_object_or_404

def home_detail(request):
    instance = get_object_or_404(Post, id=2)
    context = {
        "title": instance.title,
        "instance": instance
    }
    return render(request, "post_detail.html", context)
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code