“ WSGIRequest”对象没有属性“ thema”

时间:2019-02-07 12:11:21

标签: django

我今天已经弄清楚了很多,并获得了我的课堂观点。但我想将我的DetailViews更改为ListViews,什么效果很好。但是我无法设置查询集来正确过滤ListView,就像我在DetailsView中过滤它们的方式一样。

我总是会收到该错误:

“'WSGIRequest'对象没有属性'thema'”

我将发布代码,以便您可以看到我正在尝试做的事情:-)

models.py
class Thema(models.Model):
    themengebiet = models.CharField(max_length=350)
    beschreibung = models.TextField()
    themen_logo = models.FileField(max_length=350, upload_to="logos", default='default.jpg')
erstellt_am = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.themengebiet

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

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

    if img.height > 300 or img.width > 300:
        output_size = (300, 300)
        img.thumbnail(output_size)
        img.save(self.themen_logo.path)


class Thread(models.Model):
    thema = models.ForeignKey(Thema, on_delete=models.CASCADE)
    titel = models.CharField(max_length=350)
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    erstellt_am = models.DateTimeField(default=timezone.now)
    thread_logo = models.ImageField(upload_to="logos", default='default.jpg')

   #def get_absolute_url(self):
        #return reverse('forum:thread-page', kwargs={'pk': self.thema.id})

    def __str__(self):
       return self.titel


class Posting(models.Model):
    thread = models.ForeignKey(Thread, on_delete=models.CASCADE)
    titel = models.CharField(max_length=350)
    erstellt_am = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    inhalt = models.TextField()

    def __str__(self):
        return self.titel

views.py

class ThemenView(ListView):
    template_name = 'forum/posts_original.html'
    context_object_name = 'all_themen'
    ordering = ['-erstellt_am']
    paginate_by = 5

    def get_queryset(self):
        return Thema.objects.all()


class ThreadView(ListView):
    model = Thread
    context_object_name = 'all_threads'
    template_name = 'forum/thread.html'
    success_url = reverse_lazy('forum:posts_original')
    ordering = ['-date_posted']
    paginate_by = 5

    def get_queryset(self):
        return Thread.objects.filter(thema=self.request.thema)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。很抱歉打扰你们:def get_queryset(self):thema = get_object_or_404(Thema,pk = self.kwargs.get('pk'))return Thread.objects.filter(thema = thema).order_by('erstellt_am')< / p>