Reputation: 497
I'm trying to override the change_form.html admin template. As I enter data in the admin area, it would help me to see a few lines from the CSV file I'm entering data about.
So, in my copy of change_form.html, I've placed:
<!-- First three lines of the csv we're parsing. -->
foo: {{ csv_app.csv_sample }}
<!-- end -->
And in my model ("csv_profile"):
def csv_sample(self):
return "foo"
I've tried changeing csv_app.csv_sample to parse_csv.csv_app.csv_sample as well as a bunch of other things - but I'm not getting any results. Clearly I'm not doing this right - can anyone spot what I'm missing?
Upvotes: 1
Views: 92
Reputation: 174624
change_form.html
is used for both creating new objects and updating existing ones, so you need to make sure you display the field for existing objects only.
The more portable way to do this is to write a quick template tag, and then pass it the object_id
variable from the template.
The tag could be:
from django import template
from csv_app.models import csv_profile
register = template.Library()
@register.inclusion_tag('_tag_csv_preview.html')
def csv_preview(pk):
csv = csv_profile.get(pk=pk)
return {'preview': csv.csv_sample()}
The _tag_csv_preview.html
template simply contains {{ prevew }}
(or any other styling you want).
Then in change_form.html
:
{% load csv_app.tags %}
{% if object_id %}
{% csv_preview object_id %}
{% endif %}
You can also use another trick - which is a lot simpler. adminform.form.instance
will represent the instance for which the form is being rendered, and you then do this:
{% if adminform.form.instance %}
{{ adminform.form.instance.csv_sample }}
{% endif %}
Upvotes: 1