Django 请求.META

发布于 2024-09-14 20:44:56 字数 524 浏览 2 评论 0原文

如果我想显示 request.META 字典的多个项目:

如何将例如两个放入此字符串格式:

def myurl(request):
    return HttpResponse("You are %s" % request.META['USER'], "Your IP Adress is " % request.META['REMOTE_ADDR']) 

不起作用。

另外,我如何显示/提取该词典的选择性项目的任何想法。

如果我想通过模板运行多个。我如何将其插入 html 模板中:

例如

{{request.META }} 。这对所有人都有效吗?如何在每一行中显示它们?

如果我想要例如:

HTTP_COOKIE

QUERY_STRING

HTTP_CONNECTION

显示该 3 的最佳方式是什么?

谢谢!

If I want to display more than one item of the request.META dictionary:

How can I put e.g. two in this string format:

def myurl(request):
    return HttpResponse("You are %s" % request.META['USER'], "Your IP Adress is " % request.META['REMOTE_ADDR']) 

does not work.

Also, any ideas how I can display/extract selective items of that dictionary.

If I want to run more than one via a template. How would I insert that in the html template:

e.g.

{{request.META }} . Does that works for all? How can I display them one in each line?

if I want e.g. just:

HTTP_COOKIE

QUERY_STRING

HTTP_CONNECTION

What would be the best way to display that 3 ?

Thanks!

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

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

发布评论

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

评论(2

执手闯天涯 2024-09-21 20:44:56

更新(阅读OP对此答案的评论后)

这里的模板只是一个带有嵌入格式选项的字符串。

1) 它不必被称为 template

def myurl(request):
    place_holders = "You are %(user)s; your IP address is %(ipaddress)s"
    options = dict(user = request.META['USER'], ipaddress = request.META['REMOTE_ADDR'])
    return HttpResponse(place_holders % options)

2) 您可以完全取消它,使其内联。这纯粹是编码风格/偏好的问题。

def myurl(request):
    return HttpResponse("You are %s; your IP address is %s" % (request.META['USER'], request.META['REMOTE_ADDR']))

原始答案

对问题第一部分的快速而肮脏的回答:

def myurl(request):
    template = "You are %(user)s; your IP address is %(ipaddress)s"
    options = dict(user = request.META['USER'], ipaddress = request.META['REMOTE_ADDR'])
    return HttpResponse(template % options)

Update (after reading OP's comment to this answer)

The template here is just a string with embedded format options.

1) It doesn't have to be called template

def myurl(request):
    place_holders = "You are %(user)s; your IP address is %(ipaddress)s"
    options = dict(user = request.META['USER'], ipaddress = request.META['REMOTE_ADDR'])
    return HttpResponse(place_holders % options)

2) You can do away with it altogether make it inline. This is purely a matter of coding style/preference.

def myurl(request):
    return HttpResponse("You are %s; your IP address is %s" % (request.META['USER'], request.META['REMOTE_ADDR']))

Original Answer

Quick and dirty answer to the first part of your question:

def myurl(request):
    template = "You are %(user)s; your IP address is %(ipaddress)s"
    options = dict(user = request.META['USER'], ipaddress = request.META['REMOTE_ADDR'])
    return HttpResponse(template % options)
哭泣的笑容 2024-09-21 20:44:56

使用 RequestContext:

from django.template import RequestContext
from django.shortcuts import render_to_response


def myurl(request):
    return render_to_response('template.html', {},
                               context_instance=RequestContext(request))

然后您将可以访问 request.META 及其包含的所有内容。您可以使用调试模板标签来打印您的上下文。

Use RequestContext:

from django.template import RequestContext
from django.shortcuts import render_to_response


def myurl(request):
    return render_to_response('template.html', {},
                               context_instance=RequestContext(request))

You will then have access to request.META and everything it contains. You may use the debug template tag to print your context.

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