29

I have created a custom view.

How can I insert the view into the admin?

For a normal admin class, we can just simply register it to the admin site:

class ListAdmin(admin.ModelAdmin):
   ...

admin.site.register(List, ListAdmin)

I tried to override get_url in admin.py, question_list is the view:

class ListAdmin(admin.ModelAdmin):
    def list_view(self, request):
        return question_list(request)

    def get_urls(self):
        urls = super(ListAdmin, self).get_urls()
        list_urls = patterns('', r'^list/$', self.list_view())

        return list_urls + urls

admin.site.register(question_list, ListAdmin)

This is the question_list view:

def question_list(request):
    #questions = Question.objects.filter(topic__icontains = 1)
    questions = Question.objects.all()
    return render_to_response('admin/question_list.html', {'questions':questions})
question_list = staff_member_required(question_list)

I get 'function' object is not iterable error.

Thanks.

4
  • Can you please be more specific? Commented Apr 17, 2011 at 12:40
  • I just want to add a custom view into the site administrator. I have create a view in views.py. How can I add it into admin in admin.py? Commented Apr 17, 2011 at 12:45
  • What does the view do? You can subclass your ModelAdmin to filter the queryset etc. But usually you would render your custon view with it's individual template in the frontend. Commented Apr 17, 2011 at 12:48
  • For example I have created a view: 'code' def question_list(request): #questions = Question.objects.filter(topic__icontains = 1) questions = Question.objects.all() return render_to_response('admin/question_list.html', {'questions':questions}) 'code' How can I add this view into admin.py? Commented Apr 17, 2011 at 13:08

4 Answers 4

21

Based on the information you provided you should check this part of Django's documentation:

Adding views to admin sites (note: the link is valid for version 1.5 since version 1.3 is not supported anymore - the solution is still valid).

Then you could check this blog post and this question for some further inspiration and details.


Based on your example I really don't get why you just don't use a regular ModelAdmin with some filtering options:

class QuestionAdmin(admin.ModelAdmin):
    list_filter = ('topic',)
Sign up to request clarification or add additional context in comments.

5 Comments

The link to the blog post ist dead.
Yes, it is. "Page cannot be crawled or displayed due to robots.txt."
I edited the document to reflect a live url for the solution. However, it should be kept up-to-date since Django 1.5 could be unsupported in one or two years at most.
Updated blog post link. It is still on his site on the same URL.
6

The pattern gets a view, not the result of calling the view, i.e.:

list_urls = patterns('', r'^list/$', self.list_view())

should be

list_urls = patterns('', r'^list/$', self.list_view)

Also, "list_view" (at this stage) is a view like any other. So it will need to return an HttpResponse object.

def list_view(self, request):
    return question_list(request)

You're not showing the code for question_list() but I have the suspicion it is not returning an HttpResponse.

P.S.: If you provided the traceback of the "'function' object is not iterable" exception (you're getting this when visiting "list/" ?) there'd be less guesswork.

Comments

3

Here's an example of everything needed to add (as of Django 1.6) for a custom page that will be linked to from a button next to the "History" button in the top right of an object's detail page:

https://gist.github.com/mattlong/4b64212e096766e058b7

2 Comments

this page is not avilable now
patterns tuples are now plain old lists also the change_form.html {% block.super %} should be {{ block.super }}
0

You should override get_urls in you ModelAdmin subclass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.