Thursday, February 28, 2013

Re: Use variable in my template?

To make a variable visible in a template you must explicitly add it to the template context.  In the traditional function based view you pass a Context or RequestContext instance to the template renderer or shortcut.  The constructor for the context accepts a mapping (e.g.: a dict) whose keys are the names that will be made available in the template, and whose values will be the values in the template.  So, assuming your marke module, in your views.py you would import marke, and then, in your view function:

    c = RequestContext(request, dict(
        marke=marke,
        # other stuff, like:
        form=from,
        ...
       ))

That's just an example.  There are lots of ways to build up the context ("c" above) that you will pass to, for example, render_to_response.

Bill

On Thu, Feb 28, 2013 at 9:40 AM, Maria <mariahenkel@gmx.de> wrote:
No I dont have a problem to find the images, i already used a variable to insert images from the static folder. Its just that I cant use my "marke" variable because it is in an external .py file and I would like to learn how to use that external variable in my template.


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