Django templates Could not parse the remainder - django

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'

Related

Change signup field error message and field color

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 %}">

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 %}

django: extending allauth.account

allauth.account comes with a signup.html
{% extends "account/base.html" %}
{% load i18n %}
{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block content %}
<h1>{% trans "Sign Up" %}</h1>
<p>{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}</p>
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">{% trans "Sign Up" %} »</button>
</form>
{% endblock %}
i would like to override it instead of using the form
what do i need to post to the url account_signup?
thank you
The 'account_signup' url is aimed to receive the POST request passing
at least the form fields marked as required in order to create an account.
But keep in mind that the name of the fields may vary based on the allauth settings.
I ended up doing the same thing, ealeon. Here is what I went with and it works well. Pardon the bootstrap classes, but you get the gist.
<form class="login" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.login.errors }}
{{ form.login }}
</div>
<div class="fieldWrapper">
{{ form.password.errors }}
{{ form.password }}
</div>
<div class="login-options pull-left">
{{ form.remember.errors }}
<label for="{{ form.remember.id_for_label }}">Stay signed in?</label>
{{ form.remember }}
</div>
<div class="login-options pull-right">
<a class="button secondaryAction"
href="{% url 'account_reset_password' %}">
{% trans "Forgot Password?" %}
</a>
</div>
<button class="btn btn-block btn-primary" type="submit">{% trans "Sign In" %}</button>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
</form>

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: How to define "Next" within the Comments form

I am using the Django comments framework in two places on my site. After each submission, I'd like for the user to just be redirected back to the original page they were on.
How do you define the "next" variable so the user is redirected?
Information on the redirect : http://docs.djangoproject.com/en/dev/ref/contrib/comments/#redirecting-after-the-comment-post
Also, here is the form I am using. The comment.types do not work, but that is what I think I am supposed to do - define two different next inputs for each comment type (picture vs meal).
{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if comment.type == '19' %}
<input type="hidden" name="next" value="{% url meal comment.object_pk %}" />
{% endif %}
{% if comment.type == '23' %}
<input type="hidden" name="next" value="{% url picture comment.object_pk %}" />
{% endif %}
<!-- <input type="hidden" name="next" value="{{ next }}" /> -->
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}
{% ifequal field.name "name" %} style="display:none;"{% endifequal %}
{% ifequal field.name "email" %} style="display:none;"{% endifequal %}
{% ifequal field.name "url" %} style="display:none;"{% endifequal %}
{% ifequal field.name "title" %} style="display:none;"{% endifequal %}>
<!-- {{ field.label_tag }} -->{{ field }}
</p>
{% endif %}
{% endfor %}
<p class="submit">
<button type="submit">Send</button>
<!-- <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> -->
</p>
</form>
And then on the Meal & Picture pages I have:
<h4>Post a Message</h4>
{% render_comment_form for meal %}
<h4>Post a Message</h4>
{% render_comment_form for picture %}
Figured it out. To use the next with multiple objects, use an if statement.
{% if picture %}
<input type="hidden" name="next" value="{% url picture picture.id %}" />
{% endif %}
If you want to stay on the same page ajax is an option, you could use something like django_ajaxcomments, there are quite a few posts on others ways to do this with ajax.