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
Related
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.
I'm working on recreating the admin site to match the theme of the current website and it's been fine up till now, but when I try to select multiple users and use the "delete selected users" action nothing happens.
I have a bit of code, so I have it pasted here
The button is called in admin/actions.html on line 4.
From reading the admin-site's source I was able to find the method delete_selected in admin/actions.py.I have only been copying and pasting tags from the original admin-site templates to fit my new ones with the site's style, so I can't figure out why the method isn't being passed.
Here's apicture of the finished page, where everything works except that Go button.
I feel like there might be a way to recreate a template tag to do it for me but I'm not sure what to call or how ?
The check boxes each have the object's ID as values but I'm not sure why it's not being passed to the 'go' button there.
Sorry, it was the simplest thing. I forgot to include the form surrounding the table. I hope this question will be helpful to anyone looking to customize the admin-site in the future.
Thanks!
So, I've recently inherited a large code base that is fairly obfuscated. When I navigate a page on my local machine is there any way to determine which template/view is actually being called to create the view that I'm seeing at that moment?
I would like to get a better idea of where certain parts of the page are actually coming from, but the project is so large and disorganized that going through present templates is simply not feasible.
Is there any nice way to get around this? Worth mentioning that the defined urls all seem to be poorly written, obfuscated regex, (not to mention incredibly long) so direct examination of the urls file is not extremely feasible.
When I try to run resolve on the url of the page I'm trying to view I get a 404, and I'm not really sure where to progress from there, since the page clearly works.
Any help would be greatly appreciated.
Personnaly I use this : https://github.com/django-debug-toolbar/django-debug-toolbar
The Django Debug Toolbar is a configurable set of panels that display
various debug information about the current request/response and when
clicked, display more details about the panel's content.
Currently, the following panels have been written and are working:
Django version
Request timer
A list of settings in settings.py
Common HTTP headers
GET/POST/cookie/session variable display
Templates and context used, and their template paths
SQL queries including time to execute and links to EXPLAIN each query
List of signals, their args and receivers
Logging output via Python's built-in logging, or via the logbook module
There is also one Django management command currently:
debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell. (See example below)
If you have ideas for other panels please let us know.
Note: The Debug Toolbar only works on Django 1.3 and newer.
0 code to add, only a few minor changes to settings.py
You will get what you want and even more.
I have a new job and a huge django project (15 apps, more than 30 loc). It's pretty hard to understand it's architecture from scratch. Are there any techniques to simplify my work in the beginning? sometimes it's even hard to understand where to find a form or a view that I need... thnx in advance.
When I come to this kind of problem I open up a notebook and answer the following:
1. Infrastructure
Server configuration, OS etc
Check out the database type (mysql, postgres, nosql)
External APIS (e.g Facebook Connect)
2. Backend
Write a simple description
Write its input/output from user (try to be thorough; which fields are required and which aren't)
Write its FK and its relation to any other apps (and why)
List down each plugin the app is using. And for what purpose. For example in rails I'd write: 'gem will_paginate - To display guestbook app results on several pages'
3. Frontend
Check out the JS framework
Check the main stylesheet files (for the template)
The main html/haml (etc) files for creating a new template based page.
When you are done doing that. I think you are much more prepared and able go deeper developing/debugging the app. Good luck.
Use this http://packages.python.org/django-extensions/graph_models.html
to generate the Relationship diagrams from the models so that you can visually see how the models are related to each other. This will give you nice idea about the app
1) Try to install the site from scratch. You will find what external apps are needed for the site to run.
2) Reverse engineer. Browse through the site and try to find out what you have to do to change something to that page. Start with the url, look up in urls.py, read the view, check the model. Are there any hints to other processes?
3) Try to write down everything you don't understand, and document the answers for future reference.
I would clone the project so you can mess up endlessly.
Then I would start to reduce the code. "What happens if if just remove this function here?
Also get django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
A good terminal debugger is also golden, there are many out there, here is an example:
https://github.com/tomchristie/django-pdb
This allow you to halt the code and even inject and mutate parameters in runtime. Just like GDB in C.
If you use FireFox you can install FireBug on it and when you for example submit ajax form you can see at which url send you request after what you can easily find controller which work with this form data. At chrome this utility embedded by default and call by F12 key.
I'm new to working with Django and am developing for a client who wants to be able to change page content in the Django Admin. They need to be able to change the html of the index page without editing the files on the server.
I know about flatfiles but I'm not sure that's completely what I'm after as I can't display stuff such as Django forms for example.
EDIT: Kind of like how a CMS works but without the users/group stuff and be able to use Django View modules such as forms.
Any advice?
Thanks
Honestly, the scope of what you're looking for is too huge to cover in this format. There's a number of ways this could be done, but they're all going to require some work and customization based on the client's needs.
Flatpages could work if you allow HTML content and make sure the content is rendered as "safe" in the template. This really only covers the "content" area of the site, though. It wouldn't be wise to use flatpages for an entire site template, including header, sidebar, footer, etc.
You could create editable areas. So, you actually create models for things like headers, sidebars, footers, and modules within those areas, and then just pull them into the template as needed. Then, the client is only editing pieces of the template instead of responsible for the whole HTML document.
Forms are going to be a challenge, because they require backend-processing that requires a connected view. The client won't be able to just arbitrarily drop in some form code and have a form. But, you could use a third-party service form forms and just embed them in the available content regions. Or, there's a couple of django apps that try to implement a type of "form builder" in the admin. That might somehow let the client add a form via something like the shortcodes used in Wordpress, but you'd likely have to lay down some infrastructure to make that work.
At a certain point, stuff like this reaches a point of diminishing returns, though. The only way to allow total customization of the template is to drop down into the actual physical file and make changes there. You can make certain things easier for the client, but ultimately, they either need to scale back their customization needs or deal with the fact that they'll have to work with the filesystem.
I don't believe that is possible at this time. Of course you can edit your models but templates, I think not.
I would find out how much they need to change? If they plan a complete redesign every week then you're still looking for an answer. If they just need a dynamic front page then you can split it up into variables and let them edit sections of html. Much safer and less prone to breaking the html.