Reputation: 327
I am using Django 1.4. I would like to add a queue in admin page which should look like admin change_list page. I tried so many blogs and other forum, but did not get what I want. How can I add custom view in admin page with following features,
models.py
class Broker(models.Model):
user = models.OneToOneField(User)
agency = models.CharField(max_length=100)
urls.py
url(r'^admin/broker_list/$', 'room.admin_views.broker_list'),
url(r'^admin/', include(admin.site.urls)),
admin_views.py
def broker_list(request):
return render_to_response(
"admin/broker_list.html",
{'broker_list' : Broker.objects.filter(user__is_active=False)},
RequestContext(request, {}),
)
broker_list = staff_member_required(broker_list)
broker_list.html
{% extends "admin/base_site.html" %}
{% load admin_urls %}
{% block title %}List of pending agent{% endblock %}
{% block content %}
{{broker_list}}
{% endblock %}
With above said code, I can able to go access the link /admin/broker_list/ where I can see the queryset. But I do not know how to generate or extend like admin chagelist page.
Even I tired with Django ModelAdmin get_urls method, but the document stated that my_view should return HttpResponse, so it renders only raw object. for the reference I shared my code below,
admin.py
class ItemAdmin(admin.ModelAdmin):
model = Broker
def admin_list_broker(self, request):
print "yes iam here"
return HttpResponse("Hello!")
#return HttpResponseRedirect(
# reverse("admin:account_agent_changelist",)
#)
def get_urls(self):
from django.conf.urls.defaults import *
urls = super(ItemAdmin, self).get_urls()
my_urls = patterns('',
url(
r'broker_list',
self.admin_site.admin_view(self.admin_list_broker),
name='admin_list_broker',
),
)
print my_urls + urls
return my_urls + urls
admin.site.register(Broker, ItemAdmin)
Please help me to implement the requirement in anyone one above said method.
Thanks.
Upvotes: 1
Views: 8390
Reputation: 151
If you want a custom view to be shown at the admin site, you can use "django admin plus" Its a library that allows you to bind custom views to admin site. You can use queries and models of your choice in that view and go on further as it was'nt admin site.
Try this django library and do share with us your feedback.
Your view should look something like this after registering it to admin.py
@admin.site.register_view('pathname')
def my_view(request):
do_something
Do share your experience with this library.
Have a good one
Upvotes: 1
Reputation: 12468
Rather then creating a custom admin change view I would approach this problem by extending the built in one, the django admin provides a number of hooks to facilitate this, here are my thoughts:
You may wish to also take at look changing the change list template and admin custom actions to further customize the look and feel and to provide custom 'actions'; I have used both successfully in the past to provide project specific functionality.
Very exciting are the has_add_premission, had_change_permission
, and has_delete_permission
hooks, couple these with like django-guradian and custom admin base template could allow you to use the backend admin as a complete front end administration.
Do take the time to throughly read the entire model admin page - the better I know it the less I find myself coding custom front end administration.
Upvotes: 2