Thursday, February 28, 2013

Re: Use variable in my template?

By the way, in your marke.py, the "global" declaration doesn't do what you seem to think,
and may be a syntax error.  I know that in some other languages, "global" is used to make
a variable available to all code.  This is not the case in python.  Without the "global"
declaration, any piece of code can do:

import marke

and then reference your variable as marke.marke .

The top level variables of a module which has been imported can be referenced by the
same "." syntax as is used to reference the attributes of classes and class instances.

In python "global" is used inside of a function definition to allow the function (or method)
to write to (set the value of) a variable in the global namespace (the top level of the
module in almost all cases).  The function can read from (use the current value of) a
global (module top level) variable without using any special syntax.  But while python
is internalizing (compiling) the function definition, if it sees you setting a variable which
you have not declared global within that function body, it makes a variable that is local
to the function.  If this variable has the same name as a global variable, the global
variable is hidden from that function body.  The global declaration tells python that
you really do want to use the global variable, even if you are setting it.

Bill

On Thu, Feb 28, 2013 at 11:57 AM, Bill Freeman <ke1g.nh@gmail.com> wrote:
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