配置不当-Django

时间:2019-01-03 08:38:59

标签: django

我正在用Django开发。

但是当我尝试运行代码时,出现了ImproperlyConfigured错误...

错误:

 ImproperlyConfigured at /acl/

PermissionGroupView is missing a QuerySet. Define PermissionGroupView.model, PermissionGroupView.queryset, or override PermissionGroupView.get_queryset().

Views.py:

class PermissionGroupView(LoginRequiredMixin, generic.CreateView):
    template_name = 'acl/acl-dashboard.html'
    success_url = '/acl/'
    def get_context_data(self, **kwargs):   
        context = super(PermissionGroupView, self).get_context_data(**kwargs)
        if self.request.method == 'POST':
            context['groups'] = GroupForm(self.request.POST)
            if context['groups'].is_valid():
                context['groups'].save()
                return HttpResponseRedirect(self.get_success_url())
        else:
            context['groups'] = GroupForm()
            return context

Forms.py:

class GroupForm(forms.ModelForm):

    class Meta:
        model = Group
        fields = '__all__'

acl-dashboard.html:

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="col-md-12 grid-margin stretch-card">
    <div class="card">
      <div class="card-body">
            <h4 class="card-title">Enter New Group</h4>
        <div class="row">
            <div class="col-md-8">
                <form class="forms-sample" action="" method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    {{ groups | crispy}}
                    <button class="btn btn-success mr-2" type="submit">Save</button>
                </form>
            </div>
        </div>
      </div>
    </div>
</div>
{% endblock content %}

如果有人知道如何帮助我!

1 个答案:

答案 0 :(得分:1)

正如错误所述,您需要定义模型或查询集。所以像这样改变你的看法:

class PermissionGroupView(LoginRequiredMixin, generic.CreateView):
    model = Group  # <-- Here, we defined the model. 
    template_name = 'acl/acl-dashboard.html'
    success_url = '/acl/'

有关更多详细信息,请参见CreateView documentation

,您可以将form_class添加到视图中,这还将模型名称附加到视图的model属性中。

class PermissionGroupView(LoginRequiredMixin, generic.CreateView):
    form_class = GroupForm  # <-- Here, we defined the form. 
    template_name = 'acl/acl-dashboard.html'
    success_url = '/acl/'