Thursday, February 28, 2013

Re: Use variable in my template?

In the lines:

    return render_to_response('polls/detail.html', {'poll': p},
                               context_instance=RequestContext(request))

you have two choices.  Both:

    return render_to_response('polls/detail.html', {'poll': p, 'marke': marke},
                               context_instance=RequestContext(request))

and

    return render_to_response('polls/detail.html', {'poll': p},
                               context_instance=RequestContext(request), dict(marke=marke))

will work.  Also, in both places you have the choice of the dictionary literal syntax: {...}
or the type constructor syntax: dict(...) -- it's a matter of tast, so long as the keys are
legal python identifiers.  That is, the second example could also be written as:

    return render_to_response('polls/detail.html', {'poll': p},
                               context_instance=RequestContext(request), {'marke': marke})

The second argument of render_to_response is used to update the context_instance,
or to initialize a new one if context_instance is not provided.

As to where marke comes from, my suggestions were written from the point of view
that your marke.py worked, and that you had done:

import marke

Higher in the file.  Since I don't understand the purpose of marke (versus a constant
string, or an attribute of an image field on one of your models, I can't help you with
it.  Perhaps you could better describe why this is a variable, and from where its
contents are intended to come?

Bill

On Thu, Feb 28, 2013 at 11:30 AM, Maria <mariahenkel@gmx.de> wrote:
Can you tell me where exactly to insert that Variable?


def detail(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('polls/detail.html', {'poll': p},
                                context_instance=RequestContext(request))

Also, do I have to create a class "marke"? Because up until now I only have:


from choice import*

global marke
marke = read_m()

 
This variable reads an image name. I want to use it for {{ marke }}.jpg




On Thursday, February 28, 2013 12:06:02 PM UTC+1, Maria wrote:
Hello everyone,
 
I have a variable I want to have access to in my Django template. But somehow I can't manage to do that.
 
I got Django/mytemplates/polls/detail.html (template) where I want to insert an image:
 

{% load staticfiles %}

<html>
<head>
<link href="{% static "style.css" %}" rel="stylesheet" type="text/css" media="screen" />

</head>
<body>
 <img src="{{ STATIC_URL }}images/{{ marke.marke }}.jpg" alt="Picture" />
 
 
 
and
 
Django/mysite/polls/marke.py :
 

from choice import*

global marke
marke = read_m()

The value of marke is marke3 and I have an image marke3.jpg. I successfully used another variable to insert an image (poll.id) but this time it wont work.
The folder 'polls' is in my INSTALLED_APPS.
 
What can I do?
 
 
 
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment