Login

Django Admin inline preview

Author:
timbroder
Posted:
February 3, 2010
Language:
Python
Version:
1.1
Tags:
admin jquery
Score:
0 (after 0 ratings)

Extend this ModelAdmin to get dynamic inline previews in the list admin

also lives here: http://github.com/broderboy/django-admin-preview

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class PreviewAdmin(admin.ModelAdmin):
    def admin_slide_preview(self, obj):
        return "<div class=\"previewslide\" id=\"%s/preview/\">+</div>" % obj.id
    admin_slide_preview.allow_tags = True
    admin_slide_preview.short_description = 'Preview'
    
    def get_preview(self, request, object_id):
        sub = self.queryset(request)[0]
        template = "preview/%s.html" % sub.__class__.__name__
        return object_detail(request, queryset=self.queryset(request), object_id=object_id, template_name=template.lower())
        
    def get_urls(self):
        urls = super(PreviewAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^(?P<object_id>\d+)/preview/$', self.get_preview),
        )
        return my_urls + urls
    
    class Media:
        js = js = ('js/jquery.js',
                   'js/jquery.adminpreview.js',)

==============
jquery.adminpreview.js
$(document).ready(function(){
	$(".previewslide").click(function(){
		$.ajax({
			url:$(this).attr('id'),
			context: $(this).parent().parent(),
			success:function(data){
				var $html = $(data);
				$('.previewed').each(function(){
					$(this).remove();
				});
				
				if(!$html.hasClass('previewed')){
					$html.addClass('previewed');
				}
				
				$html.addClass($(this.context).attr('class'));		
				$(this.context).after($html);
			}
		});
	});
});



==============
preview/template.html
<tr>
	<td colspan="5">
		<table>
			<tr>
				<td>
					<img src="{{ object.image.featured.url }}">
				</td>
				<td>
					Abstract:<br/>
					{{ object.abstract }}
				</td>
				<td>
					Author: {{ object.author }}<br/>
					Published: {{ object.published_date }}<br/>
					Category: {{ object.category }}<br/>
					<br/>
					Tags: {% for tag in object.get_tags %}
							{{ tag }}
							{% if not forloop.last %}, {% endif %}
						  {% endfor %}
				</td>
			</tr>
		</table>
	</td>
</tr>

More like this

  1. Image Preview on ImageField in admin by fyaconiello 3 years, 12 months ago
  2. model save memos by depaolim 1 year, 10 months ago
  3. Inline OneToOne relations in django-admin by bjourne 4 years, 11 months ago
  4. django admin inlines readonly on change by mr-africa 8 months, 4 weeks ago
  5. Remaining character count in django admin by timbroder 5 years, 2 months ago

Comments

Please login first before commenting.