In my app.js, I have this :app.locals.testvalue = "lolo"
In my macro.html, I have this : {% macro powertestvalue(x) %} <p> {{ x }} : {{ testvalue }}</p>{% endmacro %}
The problem is I can only user the powertestvalue macro in "macro.html" because of the following :
If I use "include", I won't be able to use the powertestvalue from the file which includes macro.html .
If I use "import", macro.html won't access the global scope, and won't be able to use {{ testvalue }}.
How should I deal with this issue ?
You might find this useful: http://jinja.pocoo.org/docs/2.9/templates/#import-context-behavior
Try:
{% from 'macro.html' import powertestvalue with context %}
Related
I could find any solution about this problem. Basically I want to be able to get the data from a Nunjucks variables to be correctly rendered inside a shortcode.
{% for foo in bar %}
{% image "{{foo.src}}", "alt", "sizes", "imgClass" %}
{% endfor %}
but this results in an error
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./src/index.html (via TemplateContentRenderError)
[11ty] 2. (./src/index.html)
[11ty] Template render error: (...test.html)
[11ty] EleventyShortcodeError: Error with Nunjucks shortcode `image` (via Template render error)
[11ty] 3. ENOENT: no such file or directory, stat '{{foo.src}}.png' (via Template render error)
So it can not get the right variable value in there. But how can I do this?
You can use the variables directly in that case:
{% set images = [
{
src: "src/images/image-file.jpg",
alt: "image description",
imgClass: "center fit"
},
...
]
%}
{% for item in images %}
{% image item.src, item.alt, item.imgClass %}
{% endfor %}
I use the built-in LoginView of django2.2.7. When the virtual machine and the physical host log in separately, the users obtained in the request.user of the homepage template and the article template are inconsistent, and the session is stored in redis
模板代码如下
enter code here
{% if user.is_authenticated %}{# 用户未登录时显示 #}
欢迎,{{ user.username }}
<a>结果{{ user }}</a>
登出
{% if user.is_staff %}
站点
{% endif %}
{% else %}
注册
登录
{% endif %}</p></div>
github project address
https://github.com/13129/WEB
Project package
django==2.2.7
mysqlclient
redis
django-redis
requests
bs4
numpy
django-imagekit
django-ckeditor
django-debug-toolbar
I am trying to get the basically the active class applied to the current page. As it goes, the builder plugin is setting the URL through:
<a href="{{ detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }) }}">
However I am new to October so I am not sure how to reference this.page.id in comparison to the url set above.
Basically I want this:
{ set UrlParam = detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }
{% if this.page.id == UrlParam %} class="active" {% endif %}
Any ideas?
One of the best debugging plugins out there for OctoberCMS is this: https://octobercms.com/plugin/davask-dump
That plugin makes connecting your twig templates to your database / php rendering a breeze.
After installing that plugin you can use {{ d(variable) }} instead of {{ dd(variable) }} and get more information on the array nests etc.
So I would do {{ d(UrlParam) }} and {{ d(this.page.id) }} in your twig template. See what the dump has to say about each of those variables. For clarity I do believe you need the % here {**%** set 'variable' **%**}.
I am also not a fan of the builder component and I use the PHP section on the page / partial pages. And establishing a class with the use function and access the data with $this['variable']. Maybe worth looking into here is a quick example:
Pluginauthor\Plugin\Models\Plugin;
function onStart() {
$plugin = Pluggin::all();
$this['plugin'] = $plugin;
}
I have the following in my template code:
{% crispy form.meters_formset form.meters_formset.form.helper %}
This renders a formset that is an instance variable of my form. This is how I handle forms with embedded formsets.
In the helper for the formset's form (form.meters_formset.form.helper), I have an HTML element in the layout in which I would like to access the instance attached to that formset form. How would I do this? Crispy forms must be doing a for loop to loop through the formset's forms, but what template variable is it using?
I worked around this problem by replacing
{% crispy form.meters_formset form.meters_formset.form.helper %}
with
{% for subform in form.meters_formset %}
{% crispy subform %}
{% endfor %}
and accessing the instance with {{ subform.instance }}.
I am creating an example to learn more about using inline formsets with SessionWizard. Eventually, I want to integrate dynamic formsets in order to add and delete individual forms via the template before submitting. However, when data is absent from the second form, it fails to validate unlike like a regular ModelForm.
Is there a method within SessionWizard that needs to be overridden? Is it something that is inherently handled within Django?
Guidance and examples would be greatly appreciated.
models.py
class Parent(models.Model):
name = models.CharField(max_length=256)
def __unicode__(self):
return name
class Child(models.Model):
name = models.CharField(max_length=256)
parent = models.ForeignKey(Parent)
def __unicode__(self):
return name
urls.py
test_forms = [['parent', ParentForm],['child', ChildFormSet]]
urlpatterns = patterns('example.views',
url(r'^$', TestWizard.as_view(test_forms)),
)
forms.py
class ParentForm(ModelForm):
class Meta:
model = Parent
class ChildForm(ModelForm):
class Meta:
model = Child
exclude = ('parent',)
ChildFormSet = inlineformset_factory(Parent, Child, extra=1)
class TestWizard(SessionWizardView):
"""
This WizardView is used to create multi-page forms and handles all the
storage and validation stuff using sessions.
"""
#template_name = ''
# def get_template_names(self):
# """
# Returns a list of template names to be used for the request.
# Overridden TemplateResponseMixin for specifying template for step.
# """
# return 'survey/forms/wizard_form.html'
#
# def get_context_data(self, form, **kwargs):
# context = super(TestWizard, self).get_context_data(form=form, **kwargs)
# return context
#
# def get_form_initial(self, step):
# """
# Returns dict (list of key, values) for initial form data.
# Useful for populating form fields with data from prior form, with extra
# logic for dealing with formsets.
# """
# return self.initial_dict.get(step, {})
#
# def get_form(self, step=None, data=None, files=None):
# """
# Constructs the form for a given step - overridden to add extra arguments
# """
# form = super(TestWizard, self).get_form(step, data, files)
# return form
def done(self, form_list, **kwargs):
return render_to_response('survey/thanks.html', {
'form_data': [form.cleaned_data for form in form_list],
})
wizard-form.html
{% extends "base.html" %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<p>DEFAULT WIZARD FORM Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}