django不允许使用非英语语言的表单输入

时间:2018-10-27 08:28:12

标签: django django-forms

我的表单中有一个输入,我尝试用俄语标记,例如:

email = forms.CharField(label='ййй')

当标签仅包含非英语字符时,就会出现问题,标签会消失。
有趣的是,当标签包含至少一个英文字符时,将显示标签,例如:

email = forms.CharField(label='йййa')

正常工作。

widget参数中的属性也会发生相同的问题

email = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'ййй'}))

这是模板HTML代码:

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

这是输出的html代码:

  <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">&lt;ul&gt;&lt;li&gt;Your password can&amp;#39;t be too similar to your other personal information.&lt;/li&gt;&lt;li&gt;Your password must contain at least 8 characters.&lt;/li&gt;&lt;li&gt;Your password can&amp;#39;t be a commonly used password.&lt;/li&gt;&lt;li&gt;Your password can&amp;#39;t be entirely numeric.&lt;/li&gt;&lt;/ul&gt;</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>

1 个答案:

答案 0 :(得分:0)

找到了解决方案,只需在字符串前添加“ u”以使其成为unicode

email = forms.CharField(label=u'ййй')