Change signup field error message and field color - django

we are currently implementing a membership page. I hope the color of the field will be red if there is an error, but I don't know what to do. What should I add to my code so that it's possible?
<form method="post" class="post-form">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<input name="{{field.html_name }}" id="{{ field.id_for_lable }}" class="form-control" type="{{ field.field.widget.input_type }}" value="{{ field.value|default_if_none:'' }}"
placeholder="{{field.label}}">
{% for error in field.errors %}
<label class="control-label" for="{{ field.id_for_lable }}" style="color: #c13b2a;"><b>{{ error }}</b></label>
{% endfor %}
</div>
{% endfor %}
</div>
<div class="bbb" style="text-align: center">
<button type="submit">가입하기</button><br>
</div>

You can add stye if condition
<input ... {% if field.errors %} style={"color":"red"} {% endif %}>
or add a class if condition
<input ... class =".. {% if field.errors %} text-red {% endif %}">

Related

Django templates Could not parse the remainder

I have this template, I am looping through all choices in a question, and I want to default check the question user previously selected. selected_choice variable is coming from my view and I got have it ok in my template.
{% extends 'base.html' %}
{% block content %}
<div id="detail" class="">
<form hx-post="{% url 'main:vote' question.id %}" hx-trigger="submit" hx-target="#detail" hx-swap="outerHTML">
{% csrf_token %}
<fieldset>
<legend><h1>{{ question.question_text }}</h1></legend>
{% if error_message %}<p>
<strong>{{ error_message }}</strong>
</p>
{% endif %}
{% for choice in question.choices.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" {% if selected_choice and choice.id==selected_choice %}checked{% endif %}>
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
</fieldset>
<button type="submit">Submit</button>
</form>
</div>
{% endblock content %}
I get this error
Could not parse the remainder: '==selected_choice' from 'choice.id==selected_choice'

Set value of a select input to the value of last request in Django form template

Here is my template code
<div class="form-group row">
<label for="{{ form.diagnosis.id_for_label }}" class="col-sm-4 col-form-label col-form-label-sm">{{ form.diagnosis.label }}</label>
<div class="col-sm-8">
<input type="text" class="form-control form-control-sm{% if form.diagnosis.errors %} is-invalid{% endif %}" id="{{ form.diagnosis.id_for_label }}" name="{{ form.diagnosis.html_name }}" value="{{ form.diagnosis.value }}" required>
{% if form.diagnosis.errors %}
<div class="invalid-feedback">
{% for error in form.diagnosis.errors %}
{{ error }}
{% endfor %}
</div>
{% elif form.diagnosis.help_text %}
<small class="form-text text-muted">
{{ form.diagnosis.help_text }}
</small>
{% endif %}
</div>
</div>
<div class="form-group row">
<label for="{{ form.assigned_employee.id_for_label }}" class="col-sm-4 col-form-label col-form-label-sm">{{ form.assigned_employee.label }}</label>
<div class="col-sm-8">
<select class="custom-select custom-select-sm{% if form.assigned_employee.errors %} is-invalid{% endif %}" id="{{ form.assigned_employee.id_for_label }}" name="{{ form.assigned_employee.html_name }}">
{% for id, name in form.fields.assigned_employee.choices %}
<option value="{{ id }}"{% if form.assigned_employee.value == id %} selected{% endif %}>{{ name }}</option>
{% endfor %}
</select>
{% if form.assigned_employee.errors %}
<div class="invalid-feedback">
{% for error in form.assigned_employee.errors %}
{{ error }}
{% endfor %}
</div>
{% elif form.assigned_employee.help_text %}
<small class="form-text text-muted">
{{ form.assigned_employee.help_text }}
</small>
{% endif %}
</div>
</div>
As you can see, I have created the form template manually and would like to keep it that way.
I can set the values of previously submitted fields using {{ form.field.value }}. But I can't do the same for the <select> fields.
I have tried to do so using the following
{% for id, name in form.fields.assigned_employee.choices %}
<option value="{{ id }}"{% if form.assigned_employee.value == id %} selected{% endif %}>{{ name }}</option>
{% endfor %}
. But it doesn't work except for when I set an initial value for the field like form = SomeForm(initial={'assigned_employee': 20180011})
The interesting thing is form.assigned_employee.value returns a value after submission. I have checked it using console.log({{ form.assigned_employee.value }}).
But the evaluation of the following condition {% if form.assigned_employee.value == id %} selected{% endif %} is always false.
Does anyone have a clue what's going on?
But the evaluation of the following condition {% if form.assigned_employee.value == id %} selected{% endif %} is always false.
That's because you forget to put fields; You should use {% if form.fields.assigned_employee.value == id %} not {% if form.assigned_employee.value == id %}:
{% for id, name in form.fields.assigned_employee.choices %}
<option value="{{ id }}"{% if form.fields.assigned_employee.value == id %} selected{% endif %}>{{ name }}</option>
{% endfor %}
Edit
As mentioned in the comments by #Daniel Roseman, if you want to convert int to string in the template, you can use:
{% for id, name in form.fields.assigned_employee.choices %}
<option value="{{ id }}"{% if form.assigned_employee.value == id|stringformat:"i" %} selected{% endif %}>{{ name }}</option>
{% endfor %}

Unable to add Forgot Password link in Django Admin suit?

In my admin site, I have installed a Django suit and here I am trying to add forgot password link but can't add it Before installing the Django suit I added forgot password link and it was working perfectly but after installing suit it's not working. do you have any idea ??
login.html:
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block content %}
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %}
<div class="form-row">
{{ form.username.errors }}
{{ form.username.label_tag }} {{ form.username }}
</div>
<div class="form-row">
{{ form.password.errors }}
{{ form.password.label_tag }} {{ form.password }}
<input type="hidden" name="next" value="{{ next }}">
</div>
{% url 'admin_password_reset' as password_reset_url %}
{% if password_reset_url %}
<div class="password-reset-link">
{% trans 'Forgotten your password or username?' %}
</div>
{% endif %}
Forgot password?
<div class="submit-row">
<label> </label><input type="submit" value="{% trans 'Log in' %}">
</div>
</form>
</div>
{% endblock %}

How to create macro to clean up WTForm error checking?

Is there a better way to set a 'has-error' class within my form fields? I am using Flask and WTForms. The below code works but is very repetitive. I tried to set a dynamic variable such as {% set field + 'error' = 'has-error' %} but found out you cannot set dynamic variables in jinja.
<form action="{{ url_for_security('login') }}" method="POST" name="login_user_form">
{% if login_user_form.email.errors %}
{% set email_error = 'has-error' %}
{% endif %}
{% if login_user_form.password.errors %}
{% set password_error = 'has-error' %}
{% endif %}
<div class="form-group {{ email_error }}">
{{ login_user_form.email(class='form-control input-lg', placeholder='Email', required='true') }}
{% if login_user_form.email.errors %}
{% for error in login_user_form.email.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
{% endif %}
</div> <!-- / Email -->
<div class="form-group signin-password {{ password_error }}">
{{ login_user_form.password(class='form-control input-lg', placeholder='Password', required='true') }}
{% if login_user_form.password.errors %}
{% for error in login_user_form.password.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
{% endif %}
</div> <!-- / Password -->
<div class="form-actions">
<input type="submit" value="Log In" class="btn btn-primary btn-block btn-lg">
</div> <!-- / .form-actions -->
</form>

Django custom form template and help text

I'm trying iterate over form fields, and not want to use default {{ field }} tag. I want customise each field in cycle.
{% for field in wizard.form %}
<div class="row">
<div class="small-8 columns">
<label for="id_{{ field.html_name }}"class="inline{% if field.errors %}error {% endif %}">
{{ field.label }}
</label>
</div>
<div class="small-4 columns">
{{ field|add_error_class:"error" }}
{% if field.errors %}
<small class="error">{{ field.errors.as_text }}</small>
{% endif %}
</div>
</div>
{% endfor %}
I want to use something instead
{{ field|add_error_class:"error" }}.
Renders to:
<input class="timepicker" id="id_1-begin_time" name="1-begin_time" type="text" value="01:30:00" />
I want:
<input class="**{{ field.class }}**" id="id_{{ field.html_name }}" name="{{ field.html_name }}" type="**{{ field.type }}**" value="{{ field.value }}" />
Create tag:
from django import template
register = template.Library()
#register.filter(name='add_class')
def add_class(field, args):
return field.as_widget(attrs={"class": args})
Or in form define. Answer is here