How to add custom view and actions to django admin panel? - python

I'm new at Python and Django development. I'm trying to make a simple blog.
Right now what i am trying to do is create a admin function to show comments that not approved by admin yet. I like to show that comments and offer accept or reject choices to admin.
The problem is, i don't know the steps. I made some searches online but couldn't find what we are looking for. What are the steps to do this? What i need to learn to do this?
How do i add add custom list, buttons and functionality? I don't event know where to write code for my custom admin functionality.
Ps: I'm not looking for someone to write code for me. I'm just looking for guidelines.

I recommend using django-admin-plus (https://github.com/jsocol/django-adminplus) which does exactly what you want:
AdminPlus aims to be the smallest possible extension to the excellent Django admin component that lets you add admin views that are not tied to models.
All AdminPlus does is allow you to add simple custom views (well, they can be as complex as you like!) without mucking about with hijacking URLs, and providing links to them right in the admin index.

Related

Highly customized admin - Create an app or hack the source code?

I have been facing a big challenge with django since I was assigned the task to rebuild the admin page of our platform. I started unsure of what to do and now that I am half way on, I am even more unsure.
Here is my question:
If I want to build a highly customized admin, should I do a giant hack on the source code or create a new app?
1) Giant Hack
That is how I started and got stuck. A couple of problems appeared on the way, for instance:
I had to extend the AdminSite and override every view I wanted to change
Django's admin is very modular (I mean, very very modular) and a screen might be the sum of tons of other templates
Creating a simple navbar is a pain in the ass (at least it has been for a newbie like me). This is due to the fact that app_list (the variable that contains the mapped models to show in admin's first page) not being accessible when other templates rather than base.html are injected. Thus, I would have to create a context processor, replace every TemplateResponse() by render() (technically, since I tried to do it and it didn't work as well)
The problems go on and on. Given my lack of experience with django, I might be doing crap, but it does feel like I am on the wrong way.
this guy's answer motivated me to post this.
2) Building a brand new app
Of course it will take a little while to implement this, but it seems like a robust and maintainable way of getting it done. One of the points is that I will need to give the same flexibility as the default admin site gives when adding models and promptly having their cruds and tables (I am not really sure how to accomplish this behavior).
I am pretty new to django and any guidelines will be highly appreciated. Give me your thoughts on how to make it.
There is no need to overwrite admin views. The easiest way is to overwrite the templates and add your own css & javascripts.
I have added my own navbar, sidebars and bottom. It is really easy if you overwrite the admin base templates.
If needed you can provide custom data to the template by using own templatetags.

Viewing and filtering models: admin panel or not admin panel?

I am fairly new to Django, though I delved into other web frameworks in the past.
I am in a case where I need to view all instances of a model, be able to interactively sort and filter them through various criterias and execute an arbitratry treatment to the selected objects.
My initial approach would be to use the admin panel, since it seems to provide out of the box the features I'm looking for, plus authentication (as you can guess, I'm trying to set up a back office of some sort).
Nevertheless, I feel admin panels are essentially designed for another kind of use cases, and using them like that would be bad form (heh, "form"... nevermind). The end of this chapter of the Django book seems to confirm this feeling, too.
Is this the right way to go, or should I look into writing something from scratch?
I'd go django admin with no second thoughts. It provides 100% what you're looking for and it suits model-editing applications. Building something from scratch would be an extremely time intensive process. Also your final code would probably do things that admin already does, possibly in a less efficient way.
Even if you have no experience with the django admin application, the learning curve is not as high as developing a new interface + you will know how to use it in future projects.
I would just register the admin site with no admin prefix, and start from there :)
urlpatterns = patterns('',
(r'^', include(admin.site.urls)),
)
Side note: if you want to spice up the admin app you can check out various 3rd party applications that extend it, like grapelli or yawd-admin.
Admin in django is extremely powerful and flexible. You can define custom actions, validations, override the save methods, override default template etc. So it is advisable to modify the admin to suit your needs rather than writing one from scratch.

How do I get admin.StackedInline or admin.TabularInline like feel in my own views?

Consider the simple Poll example in the Django Tutorial example in Djnagoproject web site. Once you use admin.StackedInline or TabularInline, you get a nice interface which allows you create a neat Poll application.
If I need to provide same look, feel and functionality in my own custom views, what should I do. For eg: I want to let users create polls, but they don't have access to the admin section, but we can provide a view like "/create/polls/".
Any ideas, code, pointers will be helpful
Inline formsets.

How can I use Django admin list and filterering in my own views?

I’m just beginning to learn Django and I like the automatic listing in Django admin and the way you can configure filters and what columns to show. Is it possible to use it in my own applications?
I’ve looked in the source for the admin and figured out that I probably want to subclass the “ChangeList”-object in some way and use it in my own views. Any ideas?
You're better off doing the following.
Define a regular old Django query for your various kinds of filters. These are very easy to write.
Use the supplied generic view functions. These are very easy to use.
Create your own templates with links to your filters. You'll be building a list links based on the results of a query. For a few hard-coded cases, this is very easy. In the super-general admin-interface case, this is not simple.
Do this first. Get it to work. It won't take long. It's very important to understand Django at this level before diving into the way the admin applications work.
Later -- after you have something running -- you can spend several hours learning how the inner mysteries of the admin interface work.

Django Admin app or roll my own? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm just starting to use Django for a personal project.
What are the pros and cons of using the built-in Admin application versus integrating my administrative functions into the app itself (by checking request.user.is_staff)?
This is a community wiki because it could be considered a poll.
It really depends on the project I guess. While you can do everything in the admin, when your app gets more complex using the admin gets more complex too. And if you want to make your app really easy to manage you want control over every little detail, which is not really possible with the admin app.
I guess you should see it like this:
Using django admin: save time writing it, lose time using it.
Rolling your own admin: lose time writing it, save time using it.
I would use Django's admin app, for a number of reasons. First, writing an administration app may be quite tricky and take some time if you want to do it right, and django.contrib.admin is for free and works out of the box. Second, it is really well designed and very nice to work with (even for non-technical users). Third, it covers a lot of the common cases and it doesn't seem wise to waste time on rewriting it until you are really sure you cannot do otherwise. Fourth, it isn't really so difficult to customize. For example, adding akismet mark-as-spam and mark-as-ham buttons was really a piece of cake.
It's so easy to selectively override parts of the admin in varying degrees.
You can:
Override admin templates on an app by app basis or even model by model basis.
Override admin views by inheriting and subclassing
Catch admin url's by putting yours before it in urls.py and provide your own interfaces that are based on admin look and feel
...and lots more.
So start with the admin and then slot in whatever custom functionality you need where you need it.
There are a bunch of apps that do clever things with the admin. For example:
django-reversion takes over the
admin-log and extends it into full
history.
Tusk CMS combines the
django-mptt app with JQuery nested
sortable widget in a neat way.
Also search django-snippets for admin related snippets and this page has got a wealth of info.
I found sadly, that the while the django admin app saves a lot of time at first, it becomes a hinderence later on, as your costumer demands more features that are not easily integrated with the default admin interface. You might endup with two kinds of admin tools: the django admin (for apps that require simple data entry), and your custom rolled admin interface for other applications that require a richer interface.
Consider using the Django admin, but with your own hand-crafted widgets for particular fields. You can create sophisticated form-parts, and tell the admin to use your code for the inputs and displays of any specific field, or all fields of a type.
Jannis has done some cool stuff, and his work shows you how easy it is:
http://jannisleidel.com/2008/11/wysiwym-editor-widget-django-admin-interface/
A project that I am working on recently incorporated a time picker that uses drop-downs for the various parts of time, (h, m, s) Another field would indicate which days of the week were recurring... it would use 7 checkboxes for the days of the week, and store that in the database as a pickled dateutil.rruleset. Then, you just give the admin hints on which widgets to use for the various fields.
It involves defining your data class, your own widget which subclasses forms.Widget, your own Field which subclasses forms.Field, and a model.Field too. Each of the three classes is nice and simple and clean, and is responsible for a transition from the database to the Model, the Model to the Widget, and back through those two steps. It's really a thing of beauty.
The fields you create will be some of the most reusable, sophisticated intellectual property that you can accumulate... and you don't have to write your own admin from scratch.
I'd recommend enabling the admin site on just about every type of project. The cost of setting it up is pretty low, and it gives you a reasonably convenient mechanism for inspecting and modifying your site.
If your site has mostly a one way flow of information, from webmaster to visitors, then the admin site is probably all you need.
If, however, your site has a richer interaction among its users, you will need to compose django views that can enable that interaction while also limiting access to what users can really do.
I'd go with the Django admin functionality, over writing your own. You can customize the Django admin by adding your own templates for the admin, your own widgets, etc. I'm working on a project with a very customized Django admin. If we had decided to write it by hand, it would have taken 4 times as long to get done. I just can't see a scenario where you would want to write your own.

Categories