我在 Django 上有以下视图。我可以用通用视图做同样的事情吗?

I have the following view working on Django. Can I do the same with a Generic View?

我在 Django 上有以下视图。我可以对通用视图执行相同的操作吗?

# 型号:

风险评估 # 包含名称和信息

风险线 # 包含许多行(RiskAssessment ForeignKKey) #

def risk_view(request, pk=None):

    if  pk == None:
        return HttpResponseRedirect('/fm/riskassessment/') # No pk show list
    else:
        risk = RiskAssessment.objects.get(id = pk)

    RiskLineSet    = inlineformset_factory(RiskAssessment, RiskLine, can_delete=False, exclude=('user',))    

    if request.method == "POST":
        riskform      = RiskForm(request.POST, instance=risk)
        risklineset   = RiskLineSet(request.POST, request.FILES, instance=risk)

        if riskform.is_valid() and risklineset.is_valid():
            riskform.save()
            risklineset.save()

            # Redirect to somewhere
            if '_save' in request.POST:
                return HttpResponseRedirect('/fm/riskassessmentlist/')
            if '_addanother' in request.POST:
                return HttpResponseRedirect('/fm/riskassessmentnew/')

    else:
        riskform      = RiskForm(instance=risk)
        risklineset   = RiskLineSet(instance=risk)

    return render_to_response('formulare/RiskAssessment/risk.html', {
        'riskform'    : riskform,
        'risklineset'  : risklineset,
    }, context_instance=RequestContext(request) )

您可以尝试以下方法:

from django.views.generic import TemplateView


class RiskView(TemplateView):
    template_name = 'formulare/RiskAssessment/risk.html'
    risk = None
    RiskLineSet = None

    def dispatch(self, request, *args, **kwargs):
        if self.kwargs.get('pk', None) is None:
            return HttpResponseRedirect('/fm/riskassessment/')  # No pk show list
        else:
            self.risk = RiskAssessment.objects.get(id=pk)

        self.RiskLineSet = inlineformset_factory(RiskAssessment, RiskLine, can_delete=False, exclude=('user',))
        return super(RiskView, self).dispatch(self, request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(RiskView, self).get_context_data(**kwargs)
        context['riskform'] = RiskForm(instance=self.risk)
        context['risklineset'] = self.RiskLineSet(instance=self.risk)
        return context

    def post(self, request, *args, **kwargs):
        riskform = RiskForm(request.POST, instance=self.risk)
        risklineset = self.RiskLineSet(request.POST, request.FILES, instance=self.risk)

        if riskform.is_valid() and risklineset.is_valid():
            riskform.save()
            risklineset.save()

            # Redirect to somewhere
            if '_save' in request.POST:
                return HttpResponseRedirect('/fm/riskassessmentlist/')
            if '_addanother' in request.POST:
                return HttpResponseRedirect('/fm/riskassessmentnew/')
            # Otherwise, I guess you want to redirect to the same view
            return HttpResponseRedirect('url_of_the_current_view')

如果您有基于工作功能的视图,则不必切换到基于 class 的视图。

None 的内置通用视图是为表单集设计的。如果您使用基于 Django Class 的视图,则必须进行大量自定义。

一种选择是使用来自 django-extra-views 的 InlineFormsetView