而不将其作为参数传递

而不将其作为参数传递

本文介绍了导入“请求”视图的函数中的对象,而不将其作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拜托,我需要帮助那个令我绝望的请求对象...



我读到理论上请求在视图中是 HttpRequest对象,但我有一个脚本,需要调用一个views.py函数,需要使用该请求,所以接收它作为参数。



然后我试图在我的脚本中以这么多模式导入该对象,但似乎总是不一样并且我得到了令人讨厌的错误:

' HttpRequest'对象没有属性'session'



是否有另一种方法在视图的函数中使用request对象,避免传递参数?

这样的事情:?



please, I need help with that "request" object that is making me despair...

I read that theoretically "request" in views is an "HttpRequest" object but I have a script which needs to call a views.py function, which needs to use that "request" so receive it as argument.

Then I tried to import that object in my script in so many modes, but it seems always "not be the same" and I get annoying errors as:
"'HttpRequest' object has no attribute 'session'"

Is there another way to use "request" object in a view's function avoiding to pass as argument?
Something like this:?

def view_function():
    request = ??
    form = fooform
    bar = request.session['foo']
    context{
    'form' = form
    'bar' = bar
    }
    return render(request, "foo.html", context)





我知道这不优雅,但我还在学习,我真的不需要这个程序是一个优雅的解决方案。

提前致谢



我尝试过:



我试过在如此多的模式中导入那个神奇的对象:





I know that's not elegant, but I'm still learning and I really don't need for this program an elegant solution.
Thanks in advance

What I have tried:

I tried to import that misterious object in so many modes:

from django.http import request






,

from django.http import HttpRequest
request = HttpRequest()






,

from urllib import request






,

from urllib import HttpRequest






,

import django.http.request



...


...

推荐答案


这篇关于导入“请求”视图的函数中的对象,而不将其作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:36