I want to create a template that has some content
{% placeholder content %}
plus some "teaser" content that I want to display on another page, but not on the page that uses this specific template. Obviously, the page that displays the content does not need a teaser for it.
I'm using cmsplugin_filer and the solution I found seems way too complex:
In my template I'm defining the teaser placeholder as follows:
{% with skipteaser=True %}
{% placeholder teaser %}
{% endwith %}
and then overide the cmsplugin_filer template as follows:
Instead of
<div{% if instance.style %} class="{{ instance.style }}"{% endif %}>
<h2>{{ instance.title }}</h2>
{% if instance.image or instance.image_url %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% endif %}
{% if instance.image_url %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ instance.image_url }}"{% if instance.width or instance.height %} style="{% if instance.width %}width:{{ instance.width }}px;{% endif %}{% if instance.height %}height:{{ instance.height }}px;{% endif %}"{% endif %} />
{% else %}
{% thumbnail instance.image size crop upscale subject_location=opts.subject_location as thumbnail %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ thumbnail.url }}" />
{% endif %}
{% if link %}</a>{% endif %}
{% endif %}
{% if instance.description %}
<p>{{ instance.description }}</p>
{% endif %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% trans "more" %} »</a>{% endif %}
</div>
I use:
{% if skipteaser %}
{% else %}
<div{% if instance.style %} class="{{ instance.style }}"{% endif %}>
<h2>{{ instance.title }}</h2>
{% if instance.image or instance.image_url %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% endif %}
{% if instance.image_url %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ instance.image_url }}"{% if instance.width or instance.height %} style="{% if instance.width %}width:{{ instance.width }}px;{% endif %}{% if instance.height %}height:{{ instance.height }}px;{% endif %}"{% endif %} />
{% else %}
{% thumbnail instance.image size crop upscale subject_location=opts.subject_location as thumbnail %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ thumbnail.url }}" />
{% endif %}
{% if link %}</a>{% endif %}
{% endif %}
{% if instance.description %}
<p>{{ instance.description }}</p>
{% endif %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% trans "more" %} »</a>{% endif %}
</div>
{% endif %}
so that the teaser does not show in the page that uses the template, but I can invoke it in other pages with
{% show_placeholder "teaser" other_page %}
this seems too complex. Isn't there a way for me to define a placeholder in a template without automatically showing it?
Related
So, I found the below Django source code file on the internet.
generic_subnavigation.html
{% load common_tags %}
{% load navigation_tags %}
{% if link|common_get_type == "<class 'mayan.apps.navigation.classes.Menu'>" %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">
{% if link.icon %}{{ link.icon.render }}{% endif %}
{{ link.label }}
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
{% navigation_resolve_menu name=link.name as sub_menus_results %}
{% for sub_menu_results in sub_menus_results %}
{% for link_group in sub_menu_results.link_groups %}
{% with '' as li_class_active %}
{% with link_group.links as object_navigation_links %}
{% include 'navigation/generic_navigation.html' %}
{% endwith %}
{% endwith %}
{% endfor %}
{% endfor %}
</ul>
</li>
{% else %}
{% if as_li %}
<li class="{% if link.active and li_class_active %}{{ li_class_active }}{% endif %}">
{% endif %}
{% include link_template|default:'navigation/generic_link_instance.html' %}
{% if as_li %}
</li>
{% endif %}
{% endif %}
The code renders a webpage with sub-menus shown in the picture below;
How do I remove the "setup and tools" menu in the sub-menus shown in the picture? At least comment on it out.
generic_link_instances.html
{% if link.separator %}
<li role="separator" class="divider"></li>
{% elif link.text_span %}
<li class="text-center link-text-span {{ link.html_extra_classes }}" >{{ link.text }}</li>
{% else %}
{% if link.disabled %}
<a class="{{ link.html_extra_classes }} {% if link_classes %}{{ link_classes }} {% else %}btn {% if 'dangerous' in link.tags %}btn-danger{% else %}btn-primary{% endif %} btn-xs{% endif %} {% if link.active %}{{ link_class_active }}{% endif %} {% if 'new_window' in link.tags %}new_window{% endif %} disabled" disabled='disabled' style="cursor: default;" href="#">{% if link.icon %}{{ link.icon.render }}{% endif %} {{ link.text }}{% if link.error %} - {{ link.error }}{% endif %}</a>
{% else %}
<a
class="{{ link.html_extra_classes }} {% if link_classes %}{{ link_classes }} {% else %}btn {% if 'dangerous' in link.tags %}btn-danger{% else %}btn-primary{% endif %} btn-xs{% endif %} {% if link.active %}{{ link_class_active }}{% endif %} {% if 'new_window' in link.tags %}new_window{% endif %}"
{% for key,value in link.html_data.items %}
data-{{ key }}={{ value }}
{% endfor %}
{% if link.html_extra_attributes %}{{ link.html_extra_attributes }}{% endif %}
{% if link.url %}href="{{ link.url }}"{% endif %}
>{% if link.icon and not hide_icon %}{{ link.icon.render }}{% endif %}
{{ link.text|default:'' }}{% if link.badge_text %} <span class="badge">{{ link.badge_text }}</span>
{% endif %}{% if link.error %} - {{ link.error }}{% endif %}
</a>
{% endif %}
{% endif %}
generic_navigation.html
{% load i18n %}
{% for link in object_navigation_links %}
{% include 'navigation/generic_subnavigation.html' %}
{% endfor %}
action_dropdown.html
{% load i18n %}
{% load common_tags %}
<button aria-expanded="false" aria-haspopup="true" class="{{ action_dropdown_classes|default:"btn btn-default btn-danger"}} btn-sm dropdown-toggle" data-toggle="dropdown" type="button">
{% if action_dropdown_label %}
{{ action_dropdown_label }}
{% else %}
{% trans 'Actions' %}
{% endif %}
<span class="caret"></span>
<span class="sr-only">{% trans 'Toggle Dropdown' %}</span>
</button>
<ul class="dropdown-menu" role="menu">
{% for menus_link_result in action_menus_link_results %}
{% if not action_menu_disable_labels and action_menus_link_results|length > 1 %}
<li class="dropdown-header"><strong>{{ menus_link_result.menu.label }}</strong></li>
<li class="divider"></li>
{% endif %}
{% for link_group in menus_link_result.link_groups %}
{% if navigation_object_list %}
{% ifchanged link_group.object %}
{% common_get_object_verbose_name obj=link_group.object as link_group_object_verbose_name %}
{% if link_group_object_verbose_name %}<li class="dropdown-header">{{ link_group_object_verbose_name }}</li>{% endif %}
{% endifchanged %}
{% endif %}
{% with link_group.links as object_navigation_links %}
{% with 'true' as as_li %}
{% with 'true' as hide_active_anchor %}
{% with 'btn btn-sm navigation-btn-dropdown' as link_classes %}
{% include 'navigation/generic_navigation.html' %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% if not forloop.last and link_group %}
<li class="divider"></li>
{% endif %}
{% endfor %}
{% if not forloop.last and menus_link_result %}
<li class="divider"></li>
{% endif %}
{% endfor %}
</ul>
I have added in a template pagination I found and it works fine but wont centre on page 1 even with "text-centre" and "justify-content-center" as recommended by other answers. It looks fine on page 2 and 3... as can be seen below
{% block pagination %}
{% if is_paginated %}
<div class="text-center">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}
This is the code i used and this is what it looks like on page 1 and its pulling to the right. I currently don't have any CSS code for the pagination, though I have tried different options with no luck.
If I remove the "text-centre" then page 1 is centred but pages 2/3 pull to the left, so I am a bit stumped.
p.s this is my first django post so please let me know if it needs more information etc.
Thank you!
edit: Added this to CSS but didn't work -
.pagination {
justify-content: center;
}
tried d-flex class from a different answer, page 1 centred pages 2/3 pull left:
{% block pagination %}
{% if is_paginated %}
<div class="container">
<div class="row">
<div class="col-lg-6 offset-lg-3 py-5 border d-flex">
<ul class="pagination mx-auto">
{% if page_obj.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
</div>
</div>
</div>
Managed to figure it out, in case anyone has the same issue, i needed to use similar layout classes to those i had on the rest of the page (silly me), such as "container-wrap" and "col-md-6 col-md-offset-3 text-center":
{% block pagination %}
{% if is_paginated %}
<div class="container-wrap">
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<ul class="pagination mx-auto">
{% if page_obj.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
</div>
</div>
{% endif %}
{% endblock %}
when Making django board, I met the problem.
django version is 3.
I wanted form like this. but my code can't alert me "This field is required"
But widget tweaks form-control wasn't working.
which part is problem?
My code is like this
{% extends 'base.html' %}
{% load widget_tweaks %}
{% block title %}Start a New Topic{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item">{{ board.name }}</li>
<li class="breadcrumb-item active">New topic</li>
{% endblock %}
{% block content %}
<form method="post" novalidate>
{% csrf_token %}
{% for field in form %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">
{{ field.help_text }}
</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">Post</button>
</form>
{% endblock %}
Try using bootstrap or django crispy forms in your form.
just change your code to blow with proper indent
{% load widget_tweaks %}
{% block title %}Start a New Topic{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item">{{ board.name }}</li>
<li class="breadcrumb-item active">New topic</li>
{% endblock %}
{% block content %}
<form method="post" novalidate>
{% csrf_token %}
{% for field in form %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">
{{ field.help_text }}
</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">Post</button>
</form>
{% endblock %}```
It appears as if the uploaded images have broken links:
This is the actual error when the images are clicked:
I wanted to figure it out if i did something wrong on my list.html
{% block title %} {% if category %}{{ category.name }}{% else %}Products{% endif %} {% endblock %} {% block content %}
<div id="sidebar">
<h3>Categories</h3>
<ul>
<li {% if not category %}class="selected" {% endif %}>
All
</li>
{% for c in categories %}
<li {% if category.slug==c .slug %}class="selected" {% endif %}>
{{ c.name }}
</li>
{% endfor %}
</ul>
</div>
<div id="main" class="product-list">
<h1>{% if category %}{{ category.name }}{% else %}Products {% endif %}</h1>
{% for product in products %}
<div class="item">
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static " img/no_image.png " %}{% endif %}">
</a>
{{ product.name }}
<br> R{{ product.price }}
</div>
{% endfor %}
</div>
{% endblock %}
My views are as follows:
My models:
The quoting on this section of code in your template is wrong:
<img src="{% if product.image %} {{ product.image.url }} {% else %}
{% static 'img/no_image.png' %}{% endif %}">
I don't believe you can have conditionals like {% if %} inside double quotes. Try this instead:
{% if product.image %}
<img src="{{ product.image.url }}">
{% else %}
<img src="{% static 'img/no_image.png' %}">
{% endif %}
I am having a problem and need your help.
I am making a django website, following information need to be verified. If it pass registration will be success and fail notice will show up. But Mean while there are no problem with success, but Notice won't show up if fail. Here are my form.py, could anyone help me? Much Thanks.
if settings.VERIFY_BY_SFID and all([i in cleaned_data for i in fields]):
number = cleaned_data["mobile_number"]
verified =SignUpFailureAttemptService.verify_mobile_number(number)
if not verified:
self.add_error(None, _("Your application was rejected."))
Template
{% extends 'accounts/login_base.html' %} {% load i18n %} {% load staticfiles %} {% load semanticui %} {% load common_templatetags %} {% block welcome_text %} {% trans 'Create your personal
E-wallet account.' %} {% endblock %} {% block slogan_text %} {% trans 'Buy and sell with protection
Easy and secure to send and receive money' %} {% endblock %} {% block bigger_text %} {% blocktrans %}Already have an account?{% endblocktrans %} {% trans 'Sign in' %} {% endblock %} {% block left_content %}
STEPS:
{% include 'accounts/signup/steps.html' with active_step=step steps=5 %}
{% block form_title %} {{ form_title }} {% endblock %}
{% if messages %} {% for message in messages %}
{{ message }}
{% endfor %} {% endif %}
{% block form_block %}
{% csrf_token %} {{ form.non_field_errors }} {% for hidden_field in form.hidden_fields %} {{ hidden_field.errors }} {{ hidden_field }} {% endfor %} {% for field in form.visible_fields %} {% if field.name == 'date_of_birth' %}
{{ field.label }}*
{% endif %}
{% if field.name == 'tos' %} {% blocktrans %} I agree to {% endblocktrans %} PDS and FSG {{ field }}
{% else %} {{ field }} {% endif %}
{{ field.help_text }}
{{ field.errors }}
{% endfor %}
{% if prev_step %} « {% trans 'Back' %} {% endif %}
{% if next_step_title %}
{% trans 'NEXT STEP:' %} {{ next_step_title }}
{% endif %}
{% endblock %} {% endblock %} {% block scripts_end %} {% endblock %}