Django Crispy Form - ListView没有调用def get_queryset(self)

时间:2014-01-01 17:52:15

标签: python django django-crispy-forms

我有一个脆的形式,我想从数据库中显示其中的额外上下文。从我所看到的...... def get_queryset(self)没有被调用。

forms.py:

class LoginForm(AuthenticationForm, ListView):

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="SSO", css_class='input-xlarge'),
            Field('password', placeholder="Password", css_class='input-xlarge'),
            FormActions(
                Submit('login', 'Login', css_class="btn-primary"),
            )
        )

    model = Request
    template_name = 'requests_app/requests_list.html'
    def get_queryset(self):
        print "hi"
        return Request.objects.all()

登记/ login.html的:

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block body %}
    <div class="container" style="padding-bottom: 70px;">
        <div class='row'>
            <div class='col-md-6 col-md-offset-3'>
                <div class="well">
                    <legend>Sign in to Chrono</legend>
                    <form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-horizontal">
                        {% crispy form %}
                        <input type="hidden" name="next" value="{{ next }}"/>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {%  include 'requests_app/request_list.html' %}

{% endblock %}

requests_app / requests_list.html:

<div class="span6">
    <ul class="list-group">
        {% if object_list %}
            {% for item in  object_list  %}
            <li class="list-group-item">{{item.date_due}} - {{item.desctription}}
            <span  class="badge">
                {% if item.user_assigned %}
                <span class="badge"style="color:green">  assigned  </span>
                {% else %}<span class="badge" style="color:red">unassigned</span>
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% else %}
        <p>Yay! No maintenance requests found!</p>
        {% endif %}
</div>

1 个答案:

答案 0 :(得分:0)

您将表单视图混淆。视图处理请求和使用表单。 ListView定义了您要添加到表单的get_queryset方法。

您需要查看Django教程,尤其是第四部分:Writing Your First Django App, Part 4

- 我链接到的部分有一个ListView子类,可以帮助您。

我建议你查看整个教程。这很好。