Django管理员预览

时间:2016-01-12 10:17:49

标签: django django-admin preview

我需要"预览"管理员创建/编辑页面中的按钮。 当用户单击此按钮时,应保存表单并重定向到特定URL,用户可以在其中查看有关刚刚添加或编辑的对象的信息。

所以我有自定义的ModelAdmin类,我需要内联formset:

class InboundAdmin(admin.ModelAdmin, ListView):
    model = Inbound
    form = InboundForm
    change_form_template = 'admin/tour/inbound_form.html'
    inlines = [InboundTourDatesInline, InboundProgramInline, InboundFeedbackInline, InboundMedia,
               InboundTourSliderPhotoInline, InboundPriceDynamicsInline]

模板扩展admin/change_form.html。 此模板具有自定义按钮:

<input type="submit" value="{% trans 'Preview' %}" class="default draft-submit" name="_save_as_draft" />

这个脚本:

$('.draft-submit').on('click', function(){
                $.ajax({
                    type: "POST",
                    url: "{% url 'tours:inbound_draft' %}",
                    data: $("#{{ opts.model_name }}_form").serialize()
                });
            });

这是inbound_draft视图:

def draft_inbounds(request):
    print('inbounds')
    form = InboundForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        print('is_valid')
        form.save()
        # Here I should return specific url with pk as an attribute.
    print('not_valid')

问题是,当我点击“预览”按钮时,它会保存更改,但会将我重定向回admin中的list_view。解决问题的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

我找到了一个很好的解决方案,完全解决了我的问题。 您所需要的只是覆盖ModelAdmin类的save_model()response_change()response_add()方法。 这是我在admin.py文件中的类:

class InboundAdmin(admin.ModelAdmin, ListView):
    model = Inbound
    form = InboundForm
    change_form_template = 'admin/tour/inbound_form.html'
    inlines = [InboundTourDatesInline, InboundProgramInline, InboundFeedbackInline, InboundMedia,
               InboundTourSliderPhotoInline, InboundPriceDynamicsInline]

def save_model(self, request, obj, form, change):
    if "_draft" in request.POST:
        obj.published = False  # If it is a draft, then post should not be published
    else:
        obj.published = True
    super(InboundAdmin, self).save_model(request, obj, form, change)

def response_change(self, request, obj):
    if "_draft" in request.POST:
        return HttpResponseRedirect(reverse_lazy("tours:inbound_detail", kwargs={'pk': obj.pk}))
    else:
        return super(InboundAdmin, self).response_change(request, obj)

def response_add(self, request, obj, post_url_continue=None):
    if "_draft" in request.POST:
        return HttpResponseRedirect(reverse_lazy("tours:inbound_detail", kwargs={'pk': obj.pk}))
    else:
        return super(InboundAdmin, self).response_add(request, obj, post_url_continue=None)

如果请求包含"_draft",那么我会重定向到所需的网址,否则它会使用默认方法。

在你的模板中,你应该为savins添加按钮作为草稿:

<input type="submit" value="{% trans 'Save as a draft' %}" class="default draft-submit" name="_draft" />

那就是全部)

答案 1 :(得分:1)

当您按下submit输入时,它会提交表单,因此会将您重定向到更改列表。要使该按钮“仅限js”,请更改其类型:

<input type="button" value="{% trans 'Preview' %}" class="default draft-submit" name="_save_as_draft" />

或者你可以通过JS阻止表单提交:

$('.draft-submit').on('click', function(event){
    event.preventDefault();
    // the rest of your code
});