如果有要求,请澄清声明.django [英] Clarification on the statement if request.Post: in django

查看:36
本文介绍了如果有要求,请澄清声明.django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的看法:

def home(request, redir_url, template = 'example/home.html')
    if request.session["profile_name"] and request.session["token"]:
            return HttpResponseRedirect(redir_url)
    if request.POST:
        driver = Facebook()
        res = driver.RetLoginUrl(redir_url)
        return HttpResponseRedirect(res)

    return render(request, template)

在此视图中,首先,我检查会话变量,如果用户已经登录,则重定向到欢迎页面,如果没有,则有一个登录按钮,用户可以使用该按钮向Facebook授权我的应用.

In this view, first I check the session variables, if the user has already logged in, I redirect to the welcome page, and if not I have a login button with which the user can authorize my app with Facebook.

并且模板的表单方法为POST,将登录按钮作为输入.

And the template has a form method=POST with the login button as input.

我的问题是,当在视图末尾有一条语句返回render(request,template)时,if语句(if request.POST)如何执行.呈现页面后,即执行了return render()语句后,视图函数是否应该终止,因此不会向视图提交表单响应?

My question is, how does the if statement (if request.POST) get executed, when there is a statement return render(request, template) at the end of the view. After the page is rendered, i.e. return render() statement has been executed, shouldn't the view function terminate, hence without a form response being submitted to the view?

基本上,我只是想了解django视图的执行流程.是否首先执行return render()语句,然后等待用户输入?

Basically, I just would like to understand the execution flow of a django view. Is the return render() statement executed first, and then waits for user input?

推荐答案

应该是

if request.method == 'POST'

每当您访问为此视图配置的URL时,都会执行此语句.如果请求的方法是POST,则当用户按下按钮时,将执行 if 中的代码并返回HttpResponse

This sentence is executed every time you access the url configured for this view. If the method of the request was POST, when the user presses the button, then the code inside if is executed and a HttpResponse is returned

在该示例中,仅当方法不是POST(例如GET,PUT,DELETE等)时才执行行 render(请求,模板).

In the example the line render(request, template) is only executed when the method was'nt POST (maybe GET, PUT, DELETE, etc).

最后,您可以使用Django Login Decorator避免检查会话变量

Finally, you could use Django Login Decorator to avoid the session variables checking

这篇关于如果有要求,请澄清声明.django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆