Django admintemplates

#django #django-templates

#django #django-шаблоны

Вопрос:

У меня есть форма, которая похожа:

 class Address_info_updateForm(forms.Form):
    street = forms.CharField(label=_("Street"), required=True)
    street2 = forms.CharField(label=_("Additional Address"), required=False, max_length=50)
    zip = forms.CharField(label=_("Postcode"), required=True, max_length=50)

    city = forms.CharField(label=_("City"), required=True, max_length=50)
    state = forms.CharField(label=_("State"), required=False, max_length=50)        
    country = forms.ChoiceField(label=_("Country"), choices=countries, widget=forms.Select,    required=True)  
  

Теперь я отрисовал шаблон, который выглядит как:

 {% extends "base/base_page.html" %}
{% load i18n %}
{% load static %}

{% block html_title %}
    {% trans "Update Profile" %}                    
{% endblock %}

{% block html_page_left_content %}


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="stepcarousel.js">

stepcarousel.setup({
  galleryid: 'mygallery2', //id of carousel DIV
  beltclass: 'belt2', //class of inner "belt" DIV containing all the panel DIVs
  panelclass: 'panel2', //class of panel DIVs each holding content
  autostep: {enable:true, moveby:1, pause:3000},
  panelbehavior: {speed:500, wraparound:false, wrapbehavior:'slide', persist:true},
  defaultbuttons: {enable: true, moveby: 1, leftnav: ['{{ STATIC_PREFIX }}images/home/bigarrowleft.png', 5, 168], rightnav: ['{{ STATIC_PREFIX }}images/home/bigarrowright.png', -47, 163]},
  statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
  contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
})


</script>

<div style="height: 400px">
    <div>
        <p class="title">{% trans "BILLING ADDRESS" %}</p>
    </div>
    <form action="" method="POST" class="custom_form">{% csrf_token %}
        <table style="color:black;text-align:left; margin-left: 20px;">

           {% if form.errors %}
                                       <span style="color:red;">{% trans "You have not filled in the form correctly. Please correct the errors"%}:</span>                                                                                              
                        {% endif %}
                        {% for field in form %}
                             {% if not field.is_hidden %}        
                            <tr>
                                    <td>                                                        
                                            {{field.label}}                                                        
                                            {% if field.field.required %}
                                            <span style="color:red;">*</span>
                                            {%endif%}
                                    </td>                                                
                            </tr>
                            {%else%}                        
                                {{field}}{{field.errors}}                                                   
                            {%endif%}
                        {% endfor %}
            <tr>
                <td colspan="2">
                    <input type="submit" value="{% trans "UPDATE" %}">
                    <input type="button" value="{% trans "CANCEL" %}" onclick="document.location.href='{{base_url}}MyProfile/'" class="custom_button">
                </td>
            </tr>
        </table>
    </form>                    
    <script>
        jQuery('select#id_country').wrap('<div class="select_div" />');
    </script>
</div>
{% endblock %}
{% block html_page_right_sidebar_login %}
{% endblock %}
  

проблема в том, что когда я использую {{form.as_table}} , я вижу свои текстовые поля вместе с полями формы в очень упрощенном виде.
Но когда я использую пошаговую карусель для отображения ошибок и * отмеченных полей, она НЕ ПОКАЗЫВАЕТ МНЕ ТЕКСТОВЫЕ ПОЛЯ, а показывает мне только поля с aestrices *, подобные этому

Street * и так далее

Я хочу, чтобы поля также были aestrix вместе с текстовыми полями.

Заранее спасибо

Ответ №1:

Чтобы отобразить метку поля и текстовое поле, которые вы используете:

 {{field.label}}                                                        
{% if field.field.required %}
<span style="color:red;">*</span>
{%endif%}
  

но при этом будет отображаться только метка, {{field.label}} но не текстовое поле, потому что нет {{ field }} того, какой виджет переведен в html поля (в вашем случае это текстовое поле). Добавьте это:

 {{field.label}}                                                        
{% if field.field.required %}
<span style="color:red;">*</span>
{%endif%}
{{ field }}