views:

37

answers:

3

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

  1. 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.

  1. 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!

+1  A: 

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)
Manoj Govindan
Nice. really works. How can it work without a template you refer to?
MacPython
See updated answer. Hope this is what you meant.
Manoj Govindan
A: 

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.

Nicolas Goy
What does RequestContext do? Is that another way to get META?
MacPython
A: 

More generally, it looks like you need to read up on how to do string formatting in python. Please read http://diveintopython.org/native_data_types/formatting_strings.html

Jordan Reiter