Django admin "save and view on site"

Django's admin site is brilliant and we use it a lot in my work. My question is this: how to add an additional button at the bottom, next to the โ€œsaveโ€, โ€œsave and continue editingโ€ buttons, etc., which save the model and then are redirected to the โ€œwebsite viewโ€ button, which is available in the upper right corner shape for models with a specific?

Thanks in advance!

+4
source share
1 answer

In addition to adding a button to the change_form template, you want to override the response_change ModelAdmin (and response_add ) method.

Something like this should work:

 def response_change(self, request, obj): res = super(MyModelAdmin, self).response_change(request, obj) if "_preview" in request.POST: return HttpResponseRedirect('preview-url-here') else: return res 
+6
source

Source: https://habr.com/ru/post/1387463/


All Articles