Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

勘误:第3章 P50 关于 middleware 的 process_template_response描述疑似有误 #52

Open
GalphaXie opened this issue Mar 31, 2020 · 0 comments

Comments

@GalphaXie
Copy link

GalphaXie commented Mar 31, 2020

version : Django==1.11.29

原文描述:

执行完上述方法, 并且Django帮我们执行完view, 拿到最终的 response 后, 如果使用了模板的 response (这里指通过 return render(request, 'index.html', context={}) 方式返回的 response), 就会来到这个方法中.

实际测试后发现:

def index(request):
    students = Student.get_all()
    form = StudentForm()
    context = {
        "students": students,
        "form": form,
    }

    return render(request, 'index.html', context=context)
    # return TemplateResponse(request, "index.html", context=context)
  1. 执行上述代码, 只会进入middleware的 process_response 方法;
  2. 执行 return TemplateResponse(request, "index.html", context=context) 而非 return render(request, 'index.html', context=context) 才会进入 process_template_response 方法中

这是我的发现, 欢迎大家指出我过程中是否存在问题导致和 书籍 中 描述的结论不符.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant