在django中添加一个变量来请求

发布于 2024-10-20 22:05:02 字数 458 浏览 2 评论 0原文

在 Django 中,我想向请求添加一个变量。即,

def update_name(request):
    names = Employee.objects.filter()
    if names.count() > 0:
        request.update({"names": name })
    return render_to_response('chatlist/newchat.html',
        context_instance=RequestContext(request, {'form': form,'msg': msg}))

这是向请求添加变量的正确方法吗?如果不是,我该怎么办?

另外,如何在模板页面中检索相同的值? IE,

alert ("{{request.names['somename']}}");

In Django I want to add a variable to the request. i.e,

def update_name(request):
    names = Employee.objects.filter()
    if names.count() > 0:
        request.update({"names": name })
    return render_to_response('chatlist/newchat.html',
        context_instance=RequestContext(request, {'form': form,'msg': msg}))

Is this the right way to add a variable to the request? If not, how should I do it?

Also, how can I retrieve the same value in the templates page? i.e,

alert ("{{request.names['somename']}}");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-10-27 22:05:02

您给出的示例是错误的,因为

  1. 没有 request.update 函数
  2. 您正在使用未在任何地方分配的 name 变量?

无论如何,在Python中你可以简单地分配属性,例如

 def update_name(request):
     names = Employee.objects.filter()
     if(names.count() > 0): 
         request.names = names
 return render_to_response('chatlist/newchat.html', context_instance=RequestContext(request,{'form': form,'msg' : msg}))

你甚至不需要分配给请求,为什么你不能将它传递给模板例如

def update_name(request):
    names = Employee.objects.filter()
     return render_to_response('chatlist/newchat.html', context_instance=RequestContext(request,{'form': form,'msg' : msg, 'names': names}))

并且在模板页面中你可以访问request.name,尽管如果你是这样做只是为了在模板页面中提供一个可用的变量,这不是最好的方法,您可以将上下文字典传递给模板页面。

编辑:另请注意,在模板中使用请求之前,您需要以某种方式传递它,默认情况下它不可用,请参阅 http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext

The example you have given is wrong because

  1. there is no request.update function
  2. You are using name variable which you haven't assigned anywhere?

Anyway, in python you can simply assign attributes, e.g.

 def update_name(request):
     names = Employee.objects.filter()
     if(names.count() > 0): 
         request.names = names
 return render_to_response('chatlist/newchat.html', context_instance=RequestContext(request,{'form': form,'msg' : msg}))

Also you don't even need to assign to request, why can't you just pass it to template e.g.

def update_name(request):
    names = Employee.objects.filter()
     return render_to_response('chatlist/newchat.html', context_instance=RequestContext(request,{'form': form,'msg' : msg, 'names': names}))

And in the template page you can access request.name, though if you are doing this just to have a variable available in template page, it is not the best way, you can pass context dict to a template page.

Edit: Also note that before using request in template you need to pass it somehow, it is not available by default see http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext

橘亓 2024-10-27 22:05:02

工作代码!!

假设有一个名为 my_name 的变量。我会将其添加到 getpost 请求中。

my_name = "Nidhi"

在 Get 请求中添加变量

_mutable = request.GET._mutable
request.GET._mutable = True
request.GET['my_name'] = my_name

在 POST 请求中添加变量

_mutable = request.POST._mutable
request.POST._mutable = True
request.POST['my_name'] = my_name

现在,在请求中添加了一个名为 my_name 的变量。
要获取其值,请使用以下代码 -

request.GET.get("my_name")    # get request variable value in GET request
request.GET.get("my_name")    # get request variable value in POST request

输出:Nidhi

Working Code !!

Assume a variable named my_name. I will add this to get and post requests.

my_name = "Nidhi"

To add variable in Get request

_mutable = request.GET._mutable
request.GET._mutable = True
request.GET['my_name'] = my_name

To add variable in POST request

_mutable = request.POST._mutable
request.POST._mutable = True
request.POST['my_name'] = my_name

Now here in request there is a variable added name my_name.
To get its value use below code -

request.GET.get("my_name")    # get request variable value in GET request
request.GET.get("my_name")    # get request variable value in POST request

Output : Nidhi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文