【问题标题】:Calling a detail view inside another view if condition is true如果条件为真,则在另一个视图中调用详细视图
【发布时间】:2020-04-27 10:55:52
【问题描述】:

如果条件为真,我想在视图中调用详细视图,但我总是遇到相同的错误“上下文必须是 dict 而不是 str。”

我的代码是:

class VotacionView(LoginRequiredMixin, FormMixin, DetailView, request):

model = Votacion
form_class = realizarVotacionForm
template_name = "RealizarVotacion.html"

success_url = reverse_lazy('home')

def __init__(self, **kwargs):
    super().__init__(**kwargs)

def get_success_url(self):
    return reverse('home')

def get_context_data(self, **kwargs):
    if not self.object.voto_rectificable:
        if UsuarioVotacion.objects.filter(user=self.request.user, Votacion=self.object).exists():
            return reverse('estadisticasvotacionsimple', kwargs={'pk': self.object.pk})

more stuff...

【问题讨论】:

  • 请包含完整的回溯。

标签: python django templates view pycharm


【解决方案1】:

你不能跳出 get_context_data 方法。但你可以这样做:

def get_success_url(self):
    if self.jump:
        return reverse('estadisticasvotacionsimple', kwargs={'pk': self.object.pk})
    return reverse('home')

def get_context_data(self, **kwargs):
    if not self.object.voto_rectificable:
        if UsuarioVotacion.objects.filter(user=self.request.user, Votacion=self.object).exists():
            self.jump=True
            return super(Classname, self).get_context_data(**kwargs)
        else:
            self.jump=False
        ......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多