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 %}```
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 %}
I am using django_markdown but it is showing error when I am trying to render form. The error is on 17th line {{ form }} Error. The error is in reverse of 'django_markdown_preview', but I have included
url('^markdown/', include('django_markdown.urls')),
in my urls.py. Can anyone help me to resolve the error.
{% extends 'qaforum/base.html' %}
{% load django_markdown %}
{% block content %}
{% if message %}
<strong>Enter a valid Question!</strong>
{% endif %}
<form method="post" action="{% block action_url %}{% url 'qaforum:qaforum_create_question' %}{% endblock action_url %}">
{% csrf_token %}
{% for field in form.visible_fields %}
<tr>
<th>{{ field.label_tag }}</th>
<td>
{{ field.errors }}
{{ field }}
{{ field.help_text }}
</td>
</tr>
{% endfor %}
<input class="btn pull-right btn-success" type="submit" value="Submit Question" />
</form>
</div>
{% endblock content %}
{% block extra_js %}
{% markdown_editor "#id_description" %}
{% markdown_media %}
{% endblock extra_js %}
I have created a admin panel on the frontend and would like to show different layouts if they are on the index.html or if they are on pages after /admin e.g:
/admin/dashboard
/admin/posts
/admin/media
My if statement:
{% if request.path == "/admin" %}
It doesn't work for some reason and I can't figure out why. This is an example of the whole if:
<div class="row">
{% if request.path == "/admin" %}
<div class="col-md-full">
{% else %}
<div class="col-md-7 left">
{% block main %}{% endblock %}
</div>
{% endif %}
{% if request.path == "/admin" %}
{% trans "Empty" %}
{% else %}
<div class="col-md-3 right">
<div class="panel panel-default">
<div class="panel-body">
{% block right_panel %}
{% endblock %}
</div>
</div>
</div>
{% endif %}
</div>
what about?
{% if '/admin/' in request.path %}
I am trying to use Crispy forms with Django-userena to make it look better but when ever I put the Crispy form tags in it duplicates the form,
My code is as followed:
{% extends 'userena/base_userena.html' %}
{% load i18n %}
{% load url from future %}
{% load crispy_forms_tags %}
{% block title %}{% trans "Signin" %}{% endblock %}
{% block content %}
<form action="" method="post" class="formholder">
{% csrf_token %}
{{ form|crispy }}
<fieldset>
<legend>{% trans "Signin" %}</legend>
{{ form.non_field_errors }}
{% for field in form %}
{{ field.errors }}
{% comment %} Displaying checkboxes differently {% endcomment %}
{% if field.name == 'remember_me' %}
<p class="checkbox">
<label for="id_{{ field.name }}">{{ field }} {{ field.label }}</label>
</p>
{% else %}
<p>
{{ field.label_tag }}
{{ field }}
</p>
{% endif %}
{% endfor %}
</fieldset>
<input type="submit" value="{% trans "Signin" %}" />
<p class="forgot-password">{% trans "Forgot your password?" %}</p>
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
</form>
{% endblock %}
Solution by OP.
The problem was that the, {{ form|crispy }} was actually creating the form in the form.py file all I had to do was remove all the other from tags and just keep the {{ form|crispy }}
Here is what it looks like:
{% extends 'userena/base_userena.html' %}
{% load i18n %}
{% load url from future %}
{% block title %}{% trans "Signin" %}{% endblock %}
{% block content %}
{% load crispy_forms_tags %}
<form action="" method="post" class="formholder">
{% csrf_token %}
<fieldset>
<legend>{% trans "Signin" %}</legend>
{{ form|crispy }}
</fieldset>
<input type="submit" value="{% trans "Signin" %}" />
<p class="forgot-password">{% trans "Forgot your password?" %}</p>
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
</div>
</form>
{% endblock %}