Django {{ c }} not rendering - django-templates

Problem is that i can't get value to my template.
Views.py:
from django.shortcuts import get_object_or_404, render_to_response
from django.httpimport HttpResponse
def index(request):
c='hi'
return render_to_response('list.html', c)
list.html:
{% extends "base.html" %}
{% block content %}
list{{ c }}
{% endblock %}
It renders list but not{{ c }} what could cause this? And it gives no error..

render_to_response expects its context to be a dictionary, whereas you are passing your string directly:
def index(request):
context = { 'c': 'hi' }
return render_to_response('list.html', context)
Based on your comment below, if you want 'c' to be a list of things, it would look something like this instead:
def index(request):
context = { 'c': ['hello', 'world'] }
return render_to_response('list.html', context)
The basic idea is that you're building a mapping of the variables you want to reference in your template. The names that you reference in the template are keys in the dictionary.

Related

how to render django-taggit common tags in template

I've seen a lot of questions and answers about how to get most common tags using django-taggit. However that's not really my issue. My issue is how to pass that common tags to my django templates.
To return all tags I had:
{% for tag in model.tags.all %}
<p>{{tag}}</p>
{% endif %}
And that worked fine. But how do I return the common tags in the template? To get the common tags I have this:
class Home(ListView):
model = Model
template_name = 'home.html'
def get_common_tags(self):
common_tags = Model.tags.most_common()[:2]
return common_tags
But how do I access these common_tags? Do I need to get the common tags from the template or should I be passing them from the view?
it is better to read about Django GCBV.
in your case:
class Home(ListView):
model = Model
template_name = 'home.html'
...
def get_context_data(self, **kwargs):
kwargs['common_tags'] = self.get_common_tags()
return super().get_context_data(**kwargs)
here we override get_context_data, to get in template variable common_tags
https://docs.djangoproject.com/en/4.0/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_context_data
after that, in template:
ΒΆ
{% for tag in model.tags.all %}
<p>{{tag}}</p>
{% endif %}
<p> Most common tag are: {{ common_tags }}</p>
common_tags - is a list, you can made something with django template filter, like join:
{{ common_tags|join:" ," }}
or unordered_list:
<ul>{{ common_tags|unordered_list }}</ul>
more - here:
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#unordered-list

how to define variable in phalcon php volt blade and assign data to it?

i have a variable in my blade that its set to the blade in controller
$this->view->->setVar("formData", $formData);
my formData is an array from my submitted form
now i want to define a new variable in? my volt blade and assign my formData to it.
how should i do that?
i read phalcon(https://docs.phalcon.io/3.4/en/volt) volt document but i cant find how should i do that.
In your controller setVar() and setVars() can be used on the view object to set your variables and then use them in your Volt file:
$this->view->setVar('myData', $data);
and in the template
{{ myData }}
If $data is an array and you want elements from it:
{{ myData['element1'] }}
If $data is an object you can call methods on it
{{ myData.myMethod() }}
If you want to perform comparisons and assign variables in the template:
{% if myData['element'] == 'yes' %}
{% assign reply = true %}
{% else %}
{% assign reply = false %}
{% endif %}
References:
https://docs.phalcon.io/4.0/en/volt#variables
https://docs.phalcon.io/4.0/en/volt#assignments
https://docs.phalcon.io/4.0/en/volt#if

Django template tag can access string variable but can't call a table function?

According to https://docs.djangoproject.com/en/2.2/ref/templates/language/ you should be able to call a function from a tag as long as it takes no arguments.
I tried this:
class TaskTable(Table):
test = "test"
def give_aids():
return "aids"
def give_tuple():
return ('y', 'e', 'e', 't',)
class Meta:
template_name = 'some_template.html'
Then, in the corresponding template file, i attempted to access these like so.
{{table.test}}
{{table.give_aids}}
{% for char in table.give_tuple %}
{{char}}
{% endfor %}
Upon rendering the template "test" is displayed, while "aids" and "yeet" aren't. What am I doing wrong?
That's what I get for being a monkey at python. I'm not passing "self" to the methods. Sorry for wasting your time xD

check variable type inside Jinja2 in Flask

The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
I am completely new to Jinja2 and Flask
You are looking for the mapping test:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Jinja is not Python though, so you don't have access to all the builtins (type and print do not exist, for example, unless you add them to the context. In Flask, you do this with the context_processor decorator).
You don't actually need print at all. By default everything is output (unless you are in a child template that extends a parent, in which case you can do interesting things like the NULL Master fallback because only blocks with names available in the master template are output).
How about:
{% if {'a':1,'b':2} is mapping %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
see List of Builtin Tests for reference.
In case you want to get a custom type you can access field name like in this example:
{% if 'RelationField' in field.__class__.__name__ %}
<div class="col-md-1">
Manage object
</div>
{% endif %}

Django: user info in template

I have this in my base.html template:
{% if user.is_authenticated %}
<p style="color:#000;">
Welcome
{{ user.first_name }}
|
Logout
</p>
{% endif %}
After I authenticate, and go to this page, my first name does not show up. Can anybody tell me why?
The "Welcome" also does not show up. So, this must be failing on user.is_authenticated.
Thanks. :)
The django.contrib.auth.context_processors.auth middleware is responsible for setting request.user before a request reaches the controller.
In order to access it from a template, you need to pass this variable or a RequestContext. Example:
def something(request):
context_instance = RequestContext(request)
template_name = 'your_template.html'
extra_context = { 'other_variable': 'some value' }
return render_to_response(template_name, extra_context, context_instance)
This way, all the request variables are accessible from the template.