在使用django.views.generic类时如何解决ImproperlyConfigured错误

时间:2019-09-12 03:15:09

标签: django django-models django-forms

models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class Award_list(models.Model):
    award_id=models.AutoField(primary_key=True)
    award_name=models.CharField(max_length=50,blank=False)
    award_image = models.ImageField(default='award.jpg', upload_to='award_pics')

    def __str__(self):
        return f'{self.user.award_name} Profile'
    def save(self,*args,**kwargs):
        super().save(*args,**kwargs)

        img = Image.open(self.award_image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.award_image.path)
    class Meta:
        verbose_name_plural = 'Award'

views.py

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin,UserPassesTestMixin
from django.contrib.auth.models import User
from django.views.generic import(
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView



from . models import Award_list

    class AwardCreateView(LoginRequiredMixin,CreateView):
        models=Award_list
        fields=['award_name','award_image']
        def form_valid(self,form):
            if request.user.is_superuser:
                return super().form_valid(form)

award_list_form.html

{% extends "users/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Blog Post</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock content %}

urls.py

from django.urls import path
from .views import (
    # PostListView,
    # PostDetailView,
    AwardCreateView,
    # PostUpdateView,
    # PostDeleteView,
    # UserPostListView
)
from . import views

urlpatterns = [
    path('movies/awards/new/',AwardCreateView.as_view(),name='award-create')
]
  

在/ movies / awards / new /下错误地配置了错误   AwardCreateView缺少QuerySet。定义AwardCreateView.model,   AwardCreateView.queryset,或覆盖AwardCreateView.get_queryset()。

我尝试在URL AwardCreateView.as_view()中使用template_name并查看AwardCreateView,但是它正在得到相同的错误ImproperConfigured。

0 个答案:

没有答案