[英]Django Crispy Form - ListView is not calling def get_queryset(self)
我有一个酥脆的表格,我想在数据库中显示额外的上下文。 从我所看到的...。 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>
您将表单与视图混淆了。 视图处理请求并使用表单。 ListView
定义了要添加到表单中的get_queryset
方法。
您需要阅读Django教程,尤其是第四部分: 编写第一个Django应用程序,第4部分
—我链接到的部分有一个ListView
子类,它可以助您一臂之力。
我建议您阅读整个教程。 这很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.