B
B
BEKa T2019-04-10 14:53:50
Django
BEKa T, 2019-04-10 14:53:50

Mixin in Django?

When writing

from django.shortcuts import render
from django.shortcuts import get_object_or_404

from . models import *

class ObjectDetailMixin:
  model = None
  template = None

  def get(self, request, slug):
    obj = Tag.objects.get(slug__iexact=slug)
    return render(request, self.template, context={self.model.__name__.lower(): obj})

the page with only tags is displayed (obj = Tag.objects.get(slug__iexact=slug)) but
the page with posts does not
work
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-04-10
@Bread09

Have n't figured it out in three days ? You have two different views that receive two different slug views that work with different models, but because of the mixin, getthey have the same method . Either don't use the mixin at all, as it's useless in your case, or make it generic so that it uses the model of the CBV it's mixed in with.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question