Created
September 17, 2014 18:26
-
-
Save mattlong/4b64212e096766e058b7 to your computer and use it in GitHub Desktop.
Add a custom admin page for a model and link to it from the detail page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import update_wrapper | |
from django.contrib import admin | |
from django.contrib.admin import ModelAdmin | |
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters | |
from django.core.exceptions import PermissionDenied | |
from django.shortcuts import render | |
from myapp.models import Widget | |
from myapp.forms import ManageWidgetForm | |
class WidgetAdmin(ModelAdmin): | |
change_form_template = 'myapp/change_form.html' | |
manage_view_template = 'myapp/manage_view.html' | |
def get_urls(self): | |
from django.conf.urls import patterns, url | |
def wrap(view): | |
def wrapper(*args, **kwargs): | |
return self.admin_site.admin_view(view)(*args, **kwargs) | |
return update_wrapper(wrapper, view) | |
info = self.model._meta.app_label, self.model._meta.model_name | |
urls = patterns('', | |
url(r'^(.+)/manage/$', | |
wrap(self.manage_view), | |
name='%s_%s_manage' % info), | |
) | |
super_urls = super(WidgetAdmin, self).get_urls() | |
return urls + super_urls | |
def manage_view(self, request, id, form_url='', extra_context=None): | |
opts = Widget._meta | |
form = ManageWidgetForm() | |
obj = Widget.objects.get(pk=id) | |
if not self.has_change_permission(request, obj): | |
raise PermissionDenied | |
# do cool management stuff here | |
preserved_filters = self.get_preserved_filters(request) | |
form_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, form_url) | |
context = { | |
'title': 'Manage %s' % obj, | |
'has_change_permission': self.has_change_permission(request, obj), | |
'form_url': form_url, | |
'opts': opts, | |
'errors': form.errors, | |
'app_label': opts.app_label, | |
'original': obj, | |
} | |
context.update(extra_context or {}) | |
return render(request, self.manage_view_template, context) | |
admin.site.register(Widget, WidgetAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/change_form.html" %} | |
{% load i18n admin_urls admin_static admin_modify %} | |
{% block object-tools-items %} | |
<li> | |
{% url opts|admin_urlname:'manage' original.pk|admin_urlquote as manage_url %} | |
<a href="{% add_preserved_filters manage_url %}" class="historylink">{% trans "Manage" %}</a> | |
</li> | |
{% block.super %} | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base_site.html" %} | |
{% load i18n admin_urls admin_static admin_modify %} | |
{% load humanize tz %} | |
{% block breadcrumbs %} | |
<div class="breadcrumbs"> | |
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> | |
› <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ app_label|capfirst|escape }}</a> | |
› {% if has_change_permission %}<a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} | |
› {% trans 'Manage' %} {{ original|truncatewords:"18" }} | |
</div> | |
{% endblock %} | |
{% block content %} | |
{% timezone "America/Los_Angeles" %} | |
<div> | |
<form action="{{ form_url }}" method="post"> | |
{% csrf_token %} | |
<!-- do cool form things --> | |
</form> | |
</div> | |
{% endtimezone %} | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works fine on 1.8 except I had to change
to