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.
Related
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
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
In my online shop I want to show some more data about each product. I have managed to do this by adding an extra nodes to settings_data.json file. Like this:
{
"current": {
...
...
"7887193478": { //Product ID
"has-badge" => true,
"show-image" => false
...
},
"7887193479": { //Product ID
"has-badge" => true,
"show-image" => true
...
},
}
}
It is working and I am getting the data and showing it successfully.
The good thing about this solution is the page load is very fast.
But the problem is when some one updates the theme setting it overrides the settings_data.json file. Is there any way that I can import this extra json settings separably into my Shopify?
Please do not advise me with Metadata App. Metadata app is very slow and I am not interested to use it.
You can create a snippet products_data.liquid (or any other name you want) that stores the information you need, that's separate from the theme settings file, and won't be overwritten. Here's an example:
{% assign "7887193478-has-badge" = true %}
{% assign "7887193478-show-image" = false %}
{% assign "7887193479-has-badge" = true %}
{% assign "7887193479-show-image" = true %}
...
Then you'll want to include it somewhere in there theme.liquid file, above where you'd be using it:
{% include "products_data" %}
The downside is that it's not as clean in terms of data entry.
Using Symfony2.3.4 with Twig.
I need to assign an icon to a twig variable and then print the variable, can this be done at all??
for example:
{% set var = '<i class="glyphicon-user"></i>' %}
this obviously doesn't work, it's just to make my idea clear, it prints this text:
<i class="glyphicon-user"></i>
I need to print the icon itself.
Tips appreciated, thanks
Your method will work you just need to call the raw filter with it:
{% set var = '<i class="glyphicon-user"></i>' %}
{{ var|raw }}
Without raw twig will escape html entities
I'm not able to access template variable in TWIG macro.
Here is a simplified example:
{% set myname = "Ligio" %}
{{ _self.pagedurl(1) }}
{% macro pagedurl(page) %}
Hi {{ _self.myname }}! This is Page Num {{ page }}
{% endmacro %}
How can I access the variable myname without passing it to the macro?
You can not.
As stated in the documentation:
As PHP functions, macros don't have access to the current template
variables.
Your only solution is to pass the parameter to the macro:
{% import _self as flow %}
{{ flow.pagedurl(1, "Ligio") }}
{% macro pagedurl(page, myname) %}
Hi {{ myname }}! This is Page Num {{ page }}
{% endmacro %}
IMPORTANT NOTE:
You may have noticed in my example, I call {% import _self as flow %}.
This is something you MUST do:
When you define a macro in the template where you are going to use it,
you might be tempted to call the macro directly via _self.input()
instead of importing it; even if seems to work, this is just a
side-effect of the current implementation and it won't work anymore in
Twig 2.x.
http://twig.sensiolabs.org/doc/tags/macro.html
If you need to pass more than one global variable into the macro, you might find the _context variable useful:
{% macro mymacro(globalvars) %}
Value of the global variable pi is {{ globalvars.pi }}
{% endmacro %}
{% set pi = 3.14159 %}
{{ _self.mymacro(_context) }}
Ref: this or this answer.
You can set a global variable and access it anywhere in the template
$loader = new \Twig_Loader_Filesystem('path/to/templates');
$twig = new \Twig_Environment($loader);
$twig->addGlobal('V_Name', 'V_Value');