0

I have a django include called partner_header with the following code.

<div class="site__header header partner-header--{{ class_name }}">

How can I make that {{ class_name }} be inputted by a helper or something similar? I'm coming from a different background and am not sure of the terminology.

5
  • Can you explain better what you mean by 'a helper' and what is supposed to be the value of class_name? I don't understand how this is not a simple "How can I populate a template variable?" question. Commented Nov 6, 2015 at 18:20
  • It's probably both a "how can I populate a template variable" and "how to make that data come from an external function." In rails and meteor (the two frameworks I'm comfortable with) there's the idea of helper functions that handle data before it gets injected into your template. In this case I want the server to process some information and then pass it to the template. I don't know what that's called in django. Commented Nov 6, 2015 at 19:38
  • Ah. Just a view, then. Django is a bit weird in that it refers to models normally, but calls views 'templates' and controllers 'views' (to oversimplify). You might even have a function outside your main view that the view calls -- and you might even call it a 'helper' function -- but it's nothing special in Django. It's just using the fact that it boils down to normal Python. Commented Nov 6, 2015 at 19:40
  • Awesome, that's helpful information. So using docs.djangoproject.com/en/1.8/topics/http/views would I pass the data from my_view into the template using {{ }} or {% %} syntax? And do I have to import that at the top of the html template? Commented Nov 6, 2015 at 19:54
  • Your view is a Python function so you pass it in using a context dict (see Gocht's answer below for an example using render_to_response). In the template itself, you access using {{ var_name }} where var_name is the key in your context dict. The {% %} syntax is for control tags like for, if, etc. Commented Nov 6, 2015 at 20:05

1 Answer 1

1

Let's say you import this in your index.html file, and the corresponding view is index, if you are sending a class_name value in your context then you can use that value in your included template.

def index(request):
    ...
    return render_to_response('index.html', {'class_name': 'value'})

You included .html file is part of index.html so you can use the same variables there.

Here you will find documentation to write views and pass context (variables) to template. And if you are using Class-based views you will find docs here.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm tempted to downvote this answer because it's so unclear what the OP wants. If this is just a straight, "how do I assign a value to a template variable" question in Django, why not clean up the question to be a bit more clear and mark it duplicate?
@Two-BitAlchemist I edited the answer to provide some helpful links to write views and pass context to templates.
It's a good answer, just maybe to the wrong question. In any case I'm not going to vote either direction until we get clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.