1

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

表格.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>
4

1 回答 1

0

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

您需要查看 Django 教程,尤其是第四部分:编写您的第一个 Django 应用程序,第 4 部分

—我链接到的部分有一个ListView子类,应该可以帮助您前进。

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

于 2015-08-22T18:23:21.050 回答