django does not allow form inputs in non-english languages - django

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'ййй')

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

Is it correct use for Django ModelForm

My question; i'm used ModelForm instead of forms.Form and i want to organize form in html file instead of forms.py file (i don't want to use attr tag because my form.html file is very complicated) Is this usage correct?
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model=Comment
fields=[
'name',
'email',
'comment',
]
html file
<form class="post-comment-form" method="POST">
{% csrf_token %}
{% if form.errors %}
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
{% endif %}
<div class="form-row">
<div class="form-item half blue">
<label for="id_name" class="rl-label" >Name</label>
{{ form.name}}
</div>
<div class="form-item half blue">
<label for="id_email" class="rl-label" >Email</label>
{{ form.email}}
</div>
</div>
<div class="form-row">
<div class="form-item blue">
<label for="id_comment" class="rl-label">Comment</label>
{{ form.comment }}
</div>
</div>
<button type="submit" class="save-button">Submit</button>
If you don't want to use the {{form}} then you have to give the input field for each model's fields like <input type="text" name = "name"> .For example
<div class="form-row">
<div class="form-item half blue">
<label for="id_name" class="rl-label" >Name</label>
**<input type="text" name = "name">**
</div>
<div class="form-item half blue">
<label for="id_email" class="rl-label" >Email</label>
<input type="text" name = "email">
</div>
</div>
<div class="form-row">
<div class="form-item blue">
<label for="id_comment" class="rl-label">Comment</label>
<input type="text" name = "comment">
</div>
</div>
<button type="submit" class="save-button">Submit</button>
On the html you can have something like this
{% csrf_token %}
{{ form.as_p }}
and then proceed with styling or better yet you can use {% crispy %} which personal I find it great based on what you want to archive, your forms.py looks ok.

Django: Redirect Change Password URL

I have the following two URLs:
url(r'^change-password/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_password'),
url(r'^change-passwordiOS/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_passwordiOS'),
I thought if I used the following within the change password form it would override what URL would be loaded:
{% if 'iOS' in request.path %}
<input type="hidden" name="next" value="/profileiOS/" />
{% endif %}
But when I reach the change password from the url(r'^change-passwordiOS/$' and click the "Change Button" it does not goto the /profileiOS/ as expected but the standard /profile/ URL.
Any help would be appreciated.
/change-password/ view:
#login_required
def password_changed(request):
messages.success(request, 'Your password has been changed.')
return redirect(reverse('profile'))
Full Change Password Form:
<form class="form-horizontal" role="form" method="post" action="">
{% csrf_token %}
<fieldset>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.old_password.label }}:</label>
<div class="col-md-6">
<input name="old_password" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.old_password.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.new_password1.label }}:</label>
<div class="col-md-6">
<input name="new_password1" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.new_password1.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-6 control-label">{{ form.new_password2.label }}:</label>
<div class="col-md-6">
<input name="new_password2" type="password" class="form-control"/>
<div class="text-danger">
{% for error in form.new_password2.errors %}{{ error }}<br/>{% endfor %}
</div>
</div>
</div>
<div class="form-group">
<div class="text-right col-sm-12">
<button type="submit" class="btn btn-primary">Change Password^</button>
</div>
</div>
</fieldset>
{% if 'iOS' in request.path %}
<input type="hidden" name="next" value="/profileiOS/" />
{% endif %}
</form>
You are manually redirecting to /profile/ in password_changed view. You can change your redirect logic to depend on post param from template:
#login_required
def password_changed(request):
messages.success(request, 'Your password has been changed.')
return redirect(request.POST.get('next', reverse('profile')))

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

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