Django. Cancel button for form in admin site - django

Is there any functionality in admin site that allow to implement for every add/change form cancel button, that redirects me to list of that form objects. I mean some general solution for any form.

Add admin/submit_line.html in your project's templates directory. Use the code from the default submit_line.html, and add your cancel button. You can link it to just "../" to make it always just go up one level. Then do any necessary CSS styling to make it look right.

Create a file: yourapp/templates/admin/submit_line.html
I use Bootstrap, but you can change this easily
{% extends "admin/submit_line.html" %}
{% block submit-row %}
{{ block.super }}
Cancel
{% endblock %}
Be sure that your application is above "admin" in INSTALLED_APPS.
Looks like this in German locale:

<input type="button" name="Cancel" value="Cancel">
You can this line anywhere inside admin/submit_line.html

Related

Add button in the admin interface

I needed to add a button in the admin interface just before an Inline. What I've made is add the following in change_form:
{% block after_field_sets %}
<input type="button" value="Add contract" onClick=" window.location.href='../../contract/add/' ">
{% endblock %}
But...now the button is shown in every model page of the admin interface not just in the page of the Inline.
Any suggestion?
You can override the template for desired model only. So template should be templates/admin/my_app/my_model/change_form.html instead of simple templates/admin/change_form.html

django cms - how to make placeholders inheritable from base.html

I made in my base.html (which will be inherited from all other templates) this:
<a href="/./">
{% placeholder "Logo-Image" or %}
There is no Logo image yet.
{% endplaceholder %}
</a>
I was in Startpage and uploaded a Logo image, worked well. but once I navigated to another pages, the uploaded logo isnot there, instead i see: There is no Logo image yet.
How can I make this placeholder also inheritable?
I tried in another page this:
{% show_placeholder "Logo-Image" inherit %}
but not a single sign of success
I solved the issue. Django CMS has since version 3.0 a new tag called:
static_placeholder
to make it work:
just do in your base.html
{% static_placeholder "logo" or %}
There is no Logo image yet.
{% endstatic_placeholder %}``
and all other pages inherit this.

django adminplus link disappears when grappelli is enabled

Recently, I added adminplus which automatically creates a link on the admin page to my custom view. E.g. admin.site.register_view('somepath', 'My Fancy Admin View!', view=my_view) should produce a 'Custom View' menu with a link named 'My Fancy Admin View!'. If I disable Grappelli, the menu & link appears, however when Grappelli is enabled, the menu & link disappears. My guess is Grappelli skips this menu because it is defined differently from the rest. Any advice would be greatly appreciated.
Thank to the hint provided by dan-klasson, I found a hack for my problem
Add the following code to Grappelli's admin/index.html
{% empty %}
<p>{% trans "You don´t have permission to edit anything." %}</p>
{% endfor %}
<!-- Code above is included as point of reference -->
<!-- Add the code below -->
<div class="grp-module" id="custom_views">
<h2>Custom Views</h2>
<div class="grp-row">
{% for path, name in custom_list %}
<strong>{{ name }}</strong>
{% endfor %}
</div>
</div>

Edit Django admin logout template?

I want to make a very small change to the Django admin logout page.
I know how to use templates to override the Django admin templates, so I have tried to do the same thing with the logout file.
I have set up a new template at templates/registration/logged_out.html. The content of this file is as follows:
{% extends "registration/logged_out.html" %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
However, something is definitely wrong, because when I try to log out of admin, the site stops running.
I've found the Django docs page recommending the use of AdminSite for changes to the base template and logout pages, but is this really necessary for such a tiny change?
If so, does anyone have an example of how I might set up the logout template? I'm rather intimidated by the instructions for AdminSite.
Thanks.
The reason of manage.py runserver termination is an inheritance loop.
Django loads "registration/logged_out.html" and that it tries to load it's parent: "registration/logged_out.html". Unfortunately parent is the same template and so we end up on the template inheritance loop.
Manage.py will terminate with some variant of stack overflow error...
You can easily escape the issue by extending the parent of original "registration/logged_out.html" -> "admin/base_site.html". I.e:
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block breadcrumbs %}<div class="breadcrumbs">{% trans 'Home' %}</div>{% endblock %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
You're getting a template import loop. The template loader won't load the base template form wherever you've got Django installed, because it sees that you have that template in your project's template folder.
I think you'll need to copy the log out template from where you have Django installed to your project's template folder. Unfortunately that's the only way that seems to work. This method also means that if updates are made to the Django admin templates, you'll have to manually apply them to your modified templates.

wordpress django currenct_active_page

in wordpress your template automatically kicks out a '.current_page_item' on your menu.
I am wondering if there is a django way of doing this?
Well django is not wordpress and also not a cms, but it could be used as one.
In this case you have to do on your own, it will depends how you designed your templates?
There are many ways to do this and it depends on how you are doing your menus. I usually create my menu as a Django model. Then, in my template I compare the current path with the menu path. eg.
<ul class="menu">
{% for m in menuitems %}
<li{% if m.path == request.path }} class="current"{% endif %}>
{{ m.title }}
</li>
{% endfor %}
</ul>
Of cause, you would need to pass in menuitems into your view. To save adding in that into all my views, I usually create a template tag that fetches the menuitems variable for me.
So yes, completely possible... but it completely depends on how you decide to structure your menus and pages. Django is a web-framework, whereas Wordpress is a Blog engine.