Add links to django admin changelist view
You can also be interested in:
Sometimes you may need to add one or more links for each table row in your admin changelist view. There are many ways to do it, and here we'ss see a simple one. So this post is nothing more than a tip.
In my case I had to add a link opening a pdf view of the model item detail, here comes my implementation:
- app/models.py
from django.db import models from django.core.urlresolvers import reverse from django.utils.html import mark_safe class MyModel(models.Model): title = models.CharField('title', max_length=255) # ... def pdf_link(self): return mark_safe('<a class="grp-button" href="%s" target="blank">view pdf</a>' % reverse('archiviocr-opera-pdf', args=[self.id])) pdf_link.short_description = _('PDF')
- app/admin.py
from app.models import MyModel class MyModelAdmin(admin.ModelAdmin): list_display = ('id', 'title', 'pdf_link',) # ... admin.site.register(MyModel, MyModelAdmin)
Basically all we do is to define a method of the model which returns the html link marking it as safe (otherwise the html is encoded using entities), and using the reverse to get the url pointing to the view which generates the pdf. Such method is then added to the list_display tuple in the ModelAdmin class.
That's it, just an easy tip.
Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!
Featured

Django admin and bootstrap 5
Bootstrap 5 has come, currently in beta release, and seems already very stable.
So the question is: are you looking for ...

About code optimization, learn from exercises
Let's see an example of exercise you can face during a job interview, and let's use it to understand some ...

Notes on the Pearson correlation coefficient
The Pearson correlation coefficient is a measure of the linear correlation between two variables X and Y. It has a ...
Archive
- 2021
- 2020
- 2019
- 2018
- 2017
- Nov
- Oct
- Aug
- Jun
- Mar
- Feb
- 2016
- Oct
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2015
- Nov
- Oct
- Aug
- Apr
- Mar
- Feb
- Jan
- 2014
- Sep
- Jul
- May
- Apr
- Mar
- Feb
- Jan
- 2013
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2012
- Dec
- Nov
- Oct
- Aug
- Jul
- Jun
- May
- Apr
- Jan
- 2011
- Dec
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May