Django custom form template and help text - django

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

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

django does not allow form inputs in non-english languages

I have an input in my form which I try to label in Russian, for example:
email = forms.CharField(label='ййй')
The problem occurs when the label contains only non-English characters, the label simply disappears.
Interestingly enough, when the label contains at least one English character, the label will appear, for example:
email = forms.CharField(label='йййa')
Works correctly.
The same problem occurs with attributes in the widget parameter
email = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'ййй'}))
This the template HTML code:
{% load widget_tweaks %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<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 %}
{% if field.name == 'birth_date' %}
{% render_field field class="form-control date" id="date" name="date" %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is the output html code:
<h2>Sign up</h2>
<form method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='Q2EvMWMCPrV597OQV8aqJwtC4X1zuSQI9oVdjeZtgzQUYTAYSp5v22cz2zfOZgwn' />
<div class="form-group">
<input type="text" name="email" autofocus required class="form-control" id="id_email" />
</div>
<div class="form-group">
<label for="id_id_num">Id number:</label>
<input type="text" name="id_num" id="id_id_num" required class="form-control" maxlength="9" />
</div>
<div class="form-group">
<label for="id_password1">Password:</label>
<input type="password" name="password1" required class="form-control" id="id_password1" />
<small class="form-text text-muted"><ul><li>Your password can&#39;t be too similar to your other personal information.</li><li>Your password must contain at least 8 characters.</li><li>Your password can&#39;t be a commonly used password.</li><li>Your password can&#39;t be entirely numeric.</li></ul></small>
</div>
<div class="form-group">
<label for="id_password2">Password confirmation:</label>
<input type="password" name="password2" required class="form-control" id="id_password2" />
<small class="form-text text-muted">Enter the same password as before, for verification.</small>
</div>
<div class="form-group">
<label for="id_first_name">First name:</label>
<input type="text" name="first_name" id="id_first_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_last_name">Last name:</label>
<input type="text" name="last_name" id="id_last_name" required class="form-control" maxlength="30" />
</div>
<div class="form-group">
<label for="id_birth_date">Birth date:</label>
<input type="text" name="birth_date" required id="date" name="date" class="form-control date" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Found a solution, simply add "u" before the string to make it unicode
email = forms.CharField(label=u'ййй')

Allauth confirmation email with custom registration form?

I have custom registration form with my own registration view. I've started to using django-allauth recently and I would like to add email confirmation to my registration. Is it possible?
registration view:
def registration(request):
user_creation_form = UserProfileCreationForm(request.POST or None)
if request.method == 'POST':
if user_creation_form.is_valid():
user_creation_form.save()
username = user_creation_form.cleaned_data['username']
password = user_creation_form.cleaned_data['password2']
user = authenticate(username=username, password=password)
login(request, user)
messages.add_message(request, messages.SUCCESS, YOU_HAVE_BEEN_REGISTERED)
return HttpResponseRedirect(reverse('homepage'))
return render(request, 'dolava/accounts/registration.html',
context={'user_creation_form': user_creation_form})
This is how rewrite allauth templates:
in SETTINGS.PY
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates','allauth'),)
main app templates:
templates/account/password_reset.html (overriden allauth templates)
The problem is that I want to have many fields in registration template like type_of_user, telephone etc.
registration.html:
{% extends 'base.html' %}
{% load static %}
{% block head %}
<script src="{% static "js/registrationToggleFields.js" %}"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#id_country').select2();
$('#id_telephone_0').select2();
});
</script>
{% endblock %}
{% block content %}
<div class="col-md-12 text-center">
<h2>Register your account</h2>
<hr class="col-md-12 blackhr no-bottom-margin">
</div>
{# <div class="col-md-12 text-center center-block">#}
<form action="" method="post" class="col-md-6 col-md-offset-3" id="register-form">
{% csrf_token %}
{{ user_creation_form.non_field_errors }}
<div id="form-account-type">
<h3 align="center" class="main-color">Account Type</h3>
<hr>
{# <div class="col-md-12">#}
<div id="type-of-user-field-wrapper" class="fieldWrapper">
<div>
<label for="{{ user_creation_form.type_of_user.id_for_label }}">Type of user:</label>
{% if user_creation_form.type_of_user.field.required %} * {% endif %}
</div>
{{ user_creation_form.type_of_user }}
{{ user_creation_form.type_of_user.errors }}
</div>
<div id="country-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.country.id_for_label }}">Country:</label>
{% if user_creation_form.country.field.required %} * {% endif %}
<br>
{{ user_creation_form.country }}
{{ user_creation_form.country.errors }}
</div>
{# </div>#}
<hr>
</div>
<div id="form-login-credentials" class="form-group-container show-allways">
<h3 align="center" class="main-color">Credentials</h3>
<hr>
<div>
<div id="username-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.username.id_for_label }}">Username:</label>
{% if user_creation_form.username.field.required %} * {% endif %}
<br>
{{ user_creation_form.username }}
{{ user_creation_form.username.errors }}
</div>
<div id="password1-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.password1.id_for_label }}">Password:</label>
{% if user_creation_form.password1.field.required %} * {% endif %}
<br>
{{ user_creation_form.password1 }}
{{ user_creation_form.password1.errors }}
</div>
<div id="password2-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.password2.id_for_label }}">Confirm your password:</label>
{% if user_creation_form.password2.field.required %} * {% endif %}
<br>
{{ user_creation_form.password2 }}
{{ user_creation_form.password2.errors }}
</div>
</div>
<hr>
</div>
<div id="form-personal-information" class="form-group-container show-personal">
<h3 align="center" class="main-color">Personal information</h3>
<hr>
<div>
<div id="first-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.first_name.id_for_label }}">First name:</label>
{% if user_creation_form.first_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.first_name }}
{{ user_creation_form.first_name.errors }}
</div>
<div id="last-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.last_name.id_for_label }}">Last name:</label>
{% if user_creation_form.last_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.last_name }}
{{ user_creation_form.last_name.errors }}
</div>
</div>
<hr>
</div>
<div id="form-company-information" class="form-group-container show-company-sk show-company">
<h3 align="center" class="main-color">Company information</h3>
<hr>
<div>
<div id="company-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.company_name.id_for_label }}">Company name:</label>
{% if user_creation_form.company_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.company_name }}
{{ user_creation_form.company_name.errors }}
</div>
<div id="address-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.address.id_for_label }}">Address:</label>
{% if user_creation_form.address.field.required %} * {% endif %}
<br>
{{ user_creation_form.address }}
{{ user_creation_form.address.errors }}
</div>
</div>
<hr>
</div>
<div id="form-billing-information" class="form-group-container show-company-sk">
<h3 align="center" class="main-color">Billing information</h3>
<hr>
<div>
<div id="ico-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.ICO.id_for_label }}">IČO:</label>
{% if user_creation_form.ICO.field.required %} * {% endif %}
<br>
{{ user_creation_form.ICO }}
{{ user_creation_form.ICO.errors }}
</div>
<div id="dic-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.DIC.id_for_label }}">DIČ:</label>
{% if user_creation_form.DIC.field.required %} * {% endif %}
<br>
{{ user_creation_form.DIC }}
{{ user_creation_form.DIC.errors }}
</div>
</div>
<hr>
</div>
<div id="form-contact-information" class="form-group-container show-allways">
<h3 align="center" class="main-color">Contact information</h3>
<hr>
<div>
<div id="email-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.email.id_for_label }}">Email:</label>
{% if user_creation_form.email.field.required %} * {% endif %}
<br>
{{ user_creation_form.email }}
{{ user_creation_form.email.errors }}
</div>
<div id="telephone-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.telephone.id_for_label }}">Telephone:</label>
{% if user_creation_form.telephone.field.required %} * {% endif %}
<br>
{{ user_creation_form.telephone }}
{{ user_creation_form.telephone.errors }}
</div>
<div id="fax-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.fax.id_for_label }}">Fax:</label>
{% if user_creation_form.fax.field.required %} * {% endif %}
<br>
{{ user_creation_form.fax }}
{{ user_creation_form.fax.errors }}
</div>
</div>
</div>
<div class=" clearfix top-margin">
<button class="btn btn-success center-block" style="width:100%; padding: 15px 0;" type="submit">
Register
</button>
</div>
</form>
{# </div>#}
{% endblock %}
Do you have any advices?
You have to override one class and then supply all required variables to thhe specific template. The view that returns confirmation_email is ConfirmEmailView of allauth/account/views.py override this class, pass all the required variables from view to the template and then override your template account/email_confirm to show required data.

How do I display the number of object returned from a query

I want to display the number of objects returned from query search. I have tried
{{ p.count }} and {{ Post.count }}
Here is my post_list.html. I have read other posts that use those methods and they do not work for me. I know I am missing something.
{% extends 'posts/base.html' %}
{% block content %}
<div class="col-sm-6 col-sm-offset-3">
<h1>{{ title }}</h1>
<form method="get" action=" ">
<input type="text" name="q" placeholder="search" value="{{ request.GET.q }}"/>
<input type="submit" value=" search"/>
</form>
create
{% for p in queryset %}
<div class="row">
<div class="col-sm-12 "> <!-- i like col-sm-6 -->
<div class="thumbnail">
{% if p.image %}
<img src='{{ p.image.url }}' class="img-responsive" />
{% endif %}
<!--<img src="..." alt="...">-->
<div class="caption">
{% if p.draft %} <h3>Staff Only: Draft</h3> {% if p.publish > today %}<h3>Staff Only: Future Post</h3> {% endif %}
{% endif %}
<h3><a href='{{ p.get_absolute_url}}'>{{p.title}}</a> <small>{{p.publish | timesince }} </small> </h3>
{% if p.user.get_full_name %}<p>Author: {{ p.user.get_full_name }}</p>{% endif %}
<p>{{p.content | truncatechars:30}}</p>
<p>View
{% if user.is_authenticated %}
edit
delete
{% endif %}
</p>
</div>
</div>
</div>
<hr>
</div>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if queryset.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.
</span>
{% if queryset.has_next %}
next
{% endif %}
</span>
</div>
</div>
{% endblock content %}
Any help will be greatly appreciated. Thanks
You cannot do count on a single object p, because there's no count for that. Only queryset has count method, so do:
{{ queryset.count }}
You'll want to use the the built-in filter length
Returns the length of the value. This works for both strings and lists.
You can use it within your template to get the length of any object
{{ queryset|length }}

Register new user with curl post request

I'm trying to implement registration with my Django REST API.
I know how to login or obtain a token, so that one could do
curl -X POST -d "username=user2&password=qwer" http://localhost:8000/signin > index.html
curl -X POST -d "username=user2&password=qwer" http://localhost:8000/api-token-auth/
But what I need now is to register a new user. I tried it this way:
curl --cookie cookie.txt http://localhost:8000/signup/ -H "Content-Type: application/json" -H "X-CSRFToken: TP5mW4dxjpVO6UOk3nG6Ugk8jqv2u8E2" -X POST -d '{"username":"user4","email":"ggg#gmail.com","password":"qwer"}' >index.html
But nothing happens except I get a sign up page in my index.html file. No new user is created in the database.
This is my HTML template of sign up form. It works perfectly.
What request should I pass to my site in order to register a new user?
{% extends 'base.html' %}
{% block title %}Sign up · Parsifal{% endblock %}
{% block javascript %}
<script>
$(function () {
$('#id_username').focus();
});
</script>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Sign up for Parsifal</h3>
</div>
<div class="panel-body">
<form action="/signup/" method="post">
{% csrf_token %}
<div class="panel-group{% if form.username.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.username.id_for_label }}">{{ form.username.label }}</label>
<input type="text" class="form-control" value="{{ form.username.value|default_if_none:'' }}" id="{{ form.username.id_for_label }}" name="{{ form.username.html_name }}" maxlength="{{ form.username.field.max_length }}">
<span class="help-block">(Usernames may contain <strong>alphanumeric</strong>, <strong>_</strong> and <strong>.</strong> characters)</span>
{% for error in form.username.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.email.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.email.id_for_label }}">{{ form.email.label }}</label>
<input type="email" class="form-control" value="{{ form.email.value|default_if_none:'' }}" id="{{ form.email.id_for_label }}" name="{{ form.email.html_name }}" maxlength="{{ form.email.field.max_length }}">
{% for error in form.email.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.password.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.password.id_for_label }}">{{ form.password.label }}</label>
<input type="password" class="form-control" value="{{ form.password.value|default_if_none:'' }}" id="{{ form.password.id_for_label }}" name="{{ form.password.html_name }}">
{% for error in form.password.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="panel-group{% if form.confirm_password.errors %} has-error{% endif %}">
<label class="control-label" for="{{ form.confirm_password.id_for_label }}">{{ form.confirm_password.label }}</label>
<input type="password" class="form-control" value="{{ form.confirm_password.value|default_if_none:'' }}" id="{{ form.confirm_password.id_for_label }}" name="{{ form.confirm_password.html_name }}">
{% for error in form.confirm_password.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<button type="submit" class="btn btn-success">Create an account</button>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}