Login

Django Admin inline preview

Author:
timbroder
Posted:
February 3, 2010
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

Please login first before commenting.