Django - Admin - Mandatory fields with ' * ' - django

At present, Django admin will show all the mandatory fields with a bold labels. Is it possible mark with * in the label instead of bold labels?

The Django admin uses templates to render the add/edit page for a model. It is possible to replace that template with one of your own (which extends from the original template) overriding the template blocks you need to in order to make the changes you want to.
Check out the Django docs regarding overriding admin templates for more information.
It's the admin/change_form.html template which you would need to alter in some way (since this template renders the page shown when you add a new instance or edit an existing one). The existing templates already apply a required class to the appropriate labels, so I would create a new template which looks like this:
{% extends "admin/change_form.html" %}
{% block extrastyle %}
{{ block.super }}
<style type="text/css">
/* add an asterisk using CSS */
.required:after {
content: " *";
}
</style>
{% endblock %}
Apply to a Single Model
You should use a model admin class if you want this template to be used for specific models, setting the change_form_template attribute, as described in this section of the docs to the location of the template file you have created.
Apply to a Single App
If you want template to apply to models in an entire app create a templates folder inside the root of the app. Django will automatically look for templates there, so if you create a folder called admin and place a file in there called change_form.html it will automatically override the default Django template of that name (admin/change_form.html).
Project Wide
In order to apply this template project wide create a folder somewhere (not inside an app) called templates. Again place your new template in this directory at admin/change_form.html.
Next edit the template directories Django setting specifying the location of this directory in order to allow Django to find the template and override the default templates in the same way as before only project wide and not just app wide.
This is quite a complex set of things to do, especially for such a simple change and you may find it tricky if you have not worked with admin templates before (or even if you have).
Hopefully you now understand what is required to change an admin template, its actually fairly elagant (as is Django) but in my opinion not worth the effort just to change to some asterisks.

Related

Mezzanine: Editable tag inside header/footer template

I want to be able to use {% editable something %} inside of a layout template or a template that is included in various pages. For example a company slogan in the header or a text inside of page_menu.
I want to edit the value only on one place of administration (I don't want to have it duplicated over all pages models).
What is the best way to do this?
As commented above, the something arg can be any model instance - so all you'll need it a template tag or possibly context processor for getting the instance into every template, then voila.

How to share a plugin (the content in a sidebar widget) in several templates in Django-CMS?

I want to add a common sidebar for all my templates on my site. Let's say, I want a picture and some text, that the final user could modify whenever she feels like without having to mess with my base.html template (the base class of all my templates).
To be clear, I know that I can put plugin place holders with the template tag:
{% placeholder sidebar %}
My problem is that if I have five 5 templates and the content of the sidebar is the same for all of them, the user have to go through all the pages and change them one by one.
On the other hand, it cannot be static because I want the user to be able to modify the content through the admin.
Specifically, I am trying to do this with the cmsplugin-contact which saves me the troubles of configuring forms and emails.
You can create a special page that isn't published and add a "sidebar" placeholder to the template. You then use the {% show_placeholder %} template tag to render that sidebar placeholder in the base template that each of your 5 other pages are using

Condition that checks placeholder content existence

How can I check placeholder content existence and make next trick? :
{% if placeholder 'Head_text' %} <--check here
<div class="in">
...
...
<h2 class="title">{% placeholder 'Head_text' %}</h2>
...
...
</div>
{% endif %}
I want to know, does placeholder have some content before rendering some special HTML-structure for it.
Thanks.
I was looking for a solution for this and I found a few alternatives instead of checking if the placeholder exists. Basically the idea is to use a different plugin instead that adds the extra html. There are a few packages you can install with pip. Now, after trying them I just did it myself and it was much more easier than using the packages.
cmsplugin-text-wrapper: It integrates to the existent django-cms default text plugin keeping the editor but adds a selector on top so you can select a wrapper. The wrapper contains the extra html you would like to add. It also has a nice CSS system to add classes. On the downside, I didn't want to make the editors life more difficult with the extra template selector.
cmsplugin-text-ng: This basically adds a new plugin. When you add the plugin to your placeholder, it display a selector with the available templates (that contains your extra HTML). What I really like is that you can add extra fields that you can use in your customized HTML. For example, you could add a title so the plugin displays an extra textfield for it. On the downside, the templates are store in the database through the admin!. That is an extra hit to the database and I really don't want to sacrifice it for something too simple.
Do your own plugin using the existent Text Model. Four very simple steps:
3.1 Basically add this to your cms_plugins.py:
from cms.plugin_pool import plugin_pool
from cms.plugins.text.models import Text
from cms.plugins.text.cms_plugins import TextPlugin
class WidgetPlugin(TextPlugin):
model = Text
name = _("Widget")
render_template = "widget.html"
def render(self, context, instance, placeholder):
context['instance'] = instance
return context
plugin_pool.register_plugin(WidgetPlugin)
3.2 Create your widget.html template in your templates folder:
<div class="in">
...
...
{{ instance.body|safe }}
...
...
</div>
3.3 Place your placeholder wherever you want:
{% placeholder 'Head_text' %}
3.4 Make the user use the new plugin adding the Head_text configuration plugin in the settings.py:
CMS_PLACEHOLDER_CONF = {
#...
'Head_text': {
'plugins': {'WidgetPlugin'}
},
}
I've had this problem before, and when I researched it (this might have changed since) there is no built in way to do this, so you have to write your own template tag to load the placeholder into a variable.
Here are some discussions on the django-cms mailing list:
https://groups.google.com/forum/?fromgroups=#!topic/django-cms/QeTlmxQnn3E
https://groups.google.com/forum/#!topic/django-cms/2mWvEpTH0ns/discussion

Django CMS - Call a cmsplugin in a template tag

My problem
With Django CMS 2.3.3, when creating a Page I use cmsplugin_picture* next to a couple of other cmsplugins. In my cms template, instead of doing:
{% placholder "content" %} //calling the Django Page including all plugins...
I would like to call each cmsplugin seperately, but how would I do that?
I looked at Django tag template (filters) here and also studied Django CMS template tags here, but neither seem to suggest that possibility. I have to say I am a beginner so I might not have connected the dots...
What I try to achieve:
In my template I have a IMG tag (outside of the {% placeholder "content" %} tag) which I want to populate with an image url that I define in my Page/cmsplugin_picture. So I am looking for a placeholder tag that allows me to grab that image. In my wildest dreams I would name it:
{% show_placeholder "content" request.current_page.get_cmsplugin_picture %}
Obviously the above doesn't work, but does something like this exist?
**I have also tried cmsplugin_filer, but to me it isn't necessarely more beneficial to fix this particular problem.*
EDIT:
What I mean by Page/cmsplugin_picture -> In a Django CMS Page you can select between your installed cmsplugins to add to a Page. In my case I select cmsplugin_picture and upload an image (within that plugin). This image I want to 'call' in my Django Template. So it is a not a static url, but dynamic.
You should make a second placeholder where your img tag is (and optionally limit the types and amount of plugins using CMS_PLACEHOLDER_CONF (http://docs.django-cms.org/en/2.3.3/getting_started/configuration.html#cms-placeholder-conf).

How to modify a template of a reusable app in Django?

For example, let's say I want to modify the breadcrumbs block of the admin/change_list.html template in the Django admin. If I try to override this template like this:
{% extends "admin/change_list.html" %}
{% block breadcrumbs %} ... my changes ... {% endblock %}
then Django goes into an infinite recursion when trying to load my override, because the "extends" tag tries to load the override rather than the original template.
An obvious way would be to copy & paste the entire source template, but this is exactly the thing I'm trying to avoid. So how can I modify a template without copying and pasting all of it into my project?
The complete details on overriding the admin templates are here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates.
If you're only trying to change the template for a single app, you can simply save it as app_label/change_list.html. If you're trying to change it for a single model, you can save it as app_label/model_as_underscores/change_list.html.
If you're trying to change it for all your stuff, you can create a new template with a different name, and set its name as the change_list_template attribute on all your ModelAdmin subclasses. (On ModelAdmin, you can do the same thing with change_form_template, delete_confirmation_template, and object_history_template. On an AdminSite, you can override the index_template and login_template attributes in the same way.)
Both of these methods will allow you to extend from the original admin templates.