In my django application we are enabling our client to customize their web page. In order to do that I created a model to generate the template and it is actually working fine. My problem is that I would like to add a preview link into the admin form. This way I could do "Save and continue editing" to save the template and if I want I could click this link to preview it while editing the page.
I already activated the preview from the List display but I want this field to show into the form directly.
def get_absolute_url(self):
return "preview/%i/" % self.id
def preview_template(self):
return '<a href="' + self.get_absolute_url() + '" target="_blank">Preview</a>'
preview_template.allow_tags = True
preview_template.short_description = 'Preview'
class BlockPageAdmin(admin.ModelAdmin):
list_display = ( ... 'preview_template',)
This is working but is there a way to add it directly in the update form?
UPDATE
I got it working by following the main idea in this question: Adding an extra button to one object in django admin
I finally added the javascript file in the admin configuration of my model and I wrote a javascript function to generate the url of the preview object. I had to wrote a view to display the preview has I wished. The difference with the solution on the post was that I did a Button instead of a submit input, plus my javascript open a pop up of the preview page. So you still stay in the admin interface where you can update the model as you wish while previewing your changes.
Thank you!
You could just override the template for the change_form, or you could add a link to the object tools, which is a common place to put a "preview" link. In fact, Django admin puts the "View on site" preview link for models that have get_absolute_url defined in the same place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With