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

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.

Related

How to Replace Entire default Djnago Admin with custom Admin

I want to replace the default Django Admin templates with a completely revamped bootstrap version. The data in the Dashboard should be populated from the DB. How do I work with templated files ? Which files should be overriden ? In which template tag does HTML code go ?
From django docs on admin:
The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.
It's possible to change templates to do what you want, but that will be very complex - you have to basically rewrite most of the templates, get to know how it is working internally and probably change that too to your own design (after all, you are going to show specific details, like charts, data and so).
You will faster to achieve results if you going to build your own app, that will work exactly like you need without multiple fixes. Does it have to use js frameworks depends entirely on your requirements. Usually they are much more simple for admin backends, that are going to be used by owners, not clients. Anyway, if you don't have experience or knowledge in this, you will probably better off with just plain static django templates.
Django admin is just a same app as the ones you build, so limitations will be the same. Security wise, it doesn't offer much that you can't do yourself - make sure you have permissions set in models/views. Read more about security in django.
From functionality point of view, django admin just comes with CRUD access to your models, which can be done quite quickly. Once you past that, you will have much easier time adding functionality to your own app instead of overriding django admin.
Overall, I advise you to build your app for CMS-like admin panel. JS frameworks is neat, but that require burden of multiple additional tests (with api mocks), constant swapping between adding api / writing a component and much higher time consumption overall.

Django Editable Text Field

I've been able to easily create a list of editable items and manage them through the Django admin panel. That seems pretty simple and I have a good idea of how models work from other frameworks.
Although, I'm curious to know to know how I can make something as simple as editing a text area on a static site. Basically, I don't need to "add post" or anything of the sort. I just want to be able to edit a text area on a static site.
Any docs or examples?
I've been looking at packages such as django-flatblocks and chunks, these seem to possibly help with what I'd like to do. I'm just new to the Python world so some of this stuff is a little magic to me, especially involving the administration panel.
One thing that I realized is that these libraries (or apps in django?) seem to be super out of date, stemming back to the last commit being from 2012. Are these still commonly used?
Figured out a solution in case anyone is interested. I ended up using a Django application called django-generic-flatblocks which seems to provide me with what I need. Although, it's a bit strange as I had to replace all the text on my site with a gblock and then re-enter it all. Seems as if upon first creation of a block, it's empty so you essentially have to provide it with a value.
After that, if you log into the admin panel you're able to pull up the block and edit it. Alternatively, if you're logged into the Admin panel you're able to view your site and an edit tag is provided and you'll go directly to that block in the admin panel.
If anyone knows of anything which essentially allows me to define the text blocks up front in the admin panel then add the tags to the code, please lmk. I'd prefer to load all my content into the admin panel first and then just throw the tag into the code and have it display. That would save a lot of time in terms of having to copy the existing content, store it away, adding the tag, and then having to put it back in.
This seemed to be the only one of the recommended apps that worked for me. I tried to use Chunks because this really is only for title/text but on Django 1.11 it would freak out on me about not having South.db, which isn't even used.
https://github.com/bartTC/django-generic-flatblocks

Using Django CMS

I know python and have just read a basic intro of django. I have to built something like a travel website with real time updates. Will django be sufficent for this? Somebody advised me to look at django-CMS, I couldn't find a very beginner's tutorial there. Should I opt for django-CMS? Also how much of django should i know before i can try out django-cms?
Edit: Not too much real time stuff but just updates on the fly, like availibilty etc. Do i really need CMS?
Thanks
From your brief description it sounds like the main part of you project will be something that manages travel information and displays it to visitors to a website. This definitely sounds like something Django would be perfect for.
Django projects tend to be very modular, so the content management part of you code would likely be completely distinct from the travel parts of your project. Personally I'd start with the core travel functionality, rather than start out with worrying about content management. Then once you have that in place you'll be better positioned to decide whether django-cms fits your content needs, or whether something hand rolled will do.
Start by defining your models for the travel application. Then register those models with the admin. Get happy with how the data is modelled and then try and create one of the basic views. You should have something up and running pretty quickly.
You might also be interested in the GeoDjango project http://code.djangoproject.com/wiki/GeoDjango which provides lots of geographical and mapping tools - which sounds pretty relevant to your project.
I would say no. Django CMS is well designed, if you change content frequently. It has nice features to build up a page. But that means it only shows its benefits, when you create a lot pages/subpages and so on.
For a simple website that only presents data, without adding new pages/views, Django will suffice.
And from my experience, you should at lest be familiar with Views and URLs in order to use Django CMS well. But the same applies to Django itself. Everything else can be found on google.
Hope that helps.

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.

How do you manage your Django applications?

I just wanted to try to build a project with django. Therefore I have a (basic) question on how to manage such a project. Since I cannot find any guidelines or so on how to split a project into applications.
Let's take a kind of SO as an example. Which applications would you use?
I'd say there should be the applications "users" and "questions". But what if there was a topic system with static articles, too. Maybe they also could receive votes.
How to build the apps structure then? One app for "questions", "votes" and "topics" or just one app "content"?
I have no idea what to do. Maybe it's because I know not very much about Django yet, but I'm interested either...
There aren't hard-and-fast rules, but I would say it's better to err on the side of more specialized applications. Ideally an application should handle just one functional concern: i.e. "tagging" or "commenting" or "auth/auth" or "posts." This type of design will also help you reuse available open source applications instead of reinventing the wheel (i.e. Django comes with auth and comments apps, django-tagging or django-taggable can almost certainly do what you need, etc).
Generic foreign keys can help you decouple applications such as tagging or commenting that might be applied to models from several other applications.
You should try and separate the project in as much applications as possible. For most projects an application will not contain more than 5 models. For example a project like SO would have separate applications for UsersProfiles, Questions, Tags (there's a ready one in django for this), etc. If there was a system with static pages that'd be a separate application too (there are ready ones for this purpose). You should also try and make your applications as generic as possible, so you may reuse them in other projects. There's a good presentation on reusable apps.
Just like any set of dependencies... try to find the most useful stand-alone aspects of the project and make those stand-alone apps. Other Django Apps will have higher level functionality, and reuse the parts of the lowest level apps that you have set up.
In my project, I have a calendar app with its own Event object in its models. I also have a carpool database set up, and for the departure time and the duration I use the calendar's Event object right in my RideShare tables. The carpooling database is calendar-aware, and gets all the nice .ics export and calendar views from the calendar app for 'free.'
There are some tricks to getting the Apps reusable, like naming the templates directory: project/app2/templates/app2/index.html. This lets you refer to app2/index.html from any other app, and get the right template. I picked that one up looking at the built-in reusable apps in Django itself. Pinax is a bit of a monster size-wise but it also demonstrates a nice reusable App structure.
If in doubt, forget about reusable apps for now. Put all your messages and polls in one app and get through one rev. You'll discover during the process what steps feel unnecessary, and could be broken out as something stand-alone in the future.
A good question to ask yourself when deciding whether or not to write an app is "could I use this in another project?". If you think you could, then consider what it would take to make the application as independent as possible; How can you reduce the dependancies so that the app doesn't rely on anything specific to a particular project.
Some of the ways you can do this are:
Giving each app its own urls.py
Allowing model types to be passed in as parameters rather than explicitly declaring what models are used in your views. Generic views use this principle.
Make your templates easily overridden by having some sort of template_name parameter passed in your urls.py
Make sure you can do reverse url lookups with your objects and views. This means naming your views in the urls.py and creating get_absolute_url methods on your models.
In some cases like Tagging, GenericForeignKeys can be used to associate a model in your app to any other model, regardless of whether it has ForeignKeys "looking back" at it.
I'll tell you how I am approaching such question: I usually sit with a sheet of paper and draw the boxes (functionalities) and arrows (interdependencies between functionalities). I am sure there are methodologies or other things that could help you, but my approach usually works for me (YMMV, of course).
Knowing what a site is supposed to be is basic, though. ;)

Categories