Mezzanine: Editable tag inside header/footer template - django

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.

Related

Can we enter html code in a field in models in django?

I am making a small static website in which I have a template in which I tend to show the privacy policy terms of usage etc. I currently don't have any matter for it and tend to add it in future after deploying the site on server. I wanted to know that if I can in future add the matter on that page through a model i.e I create a model with two fields privacy policy , terms and in and pass it to the template as context in views.py . But I have a concern that the fields will have several headings which I will have to display in bold , so is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
So is there any way that I can pass html tags in model field and when I render it in my template as {{privacy}} the part I want in bold or any other style comes as that style.
Yes. You only need to tell the Django template engine not to escape the characters (for example translate < to <). You can do this with the |safe template tag [Django-doc]:
{{ privacy|safe }}

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

Django's equivalence of ASP.NET UserControl

If anyone here is ASP.NET pro, you might know what I mean by user control. I wish to create a similar one in django instead.
So, my problem is that I have several pages in my website, but I need a search bar to appear in every pages. Since I require the views.py to operate this search bar, I cannot do a simple method of
{% include 'something.html' %}
Therefore, can anyone suggest how can I do it?
There are a couple of ways to accomplish what you're wanting to do:
Context Processors
Template Tags
Context Processors can augment the template context with values, regardless of which template is loaded. They are akin to filters in Rails.
Template Tags, like Context Processors, can accomplish anything you can do in Python, but are implemented at the template level.
If you need something to be present on every template, one of the simplest ways to accomplish this is with an inclusion tag, which can also accept values passed to it. An inclusion tag could be implemented at your highest level template, a.k.a your MasterPage, and as long as you don't put it in a block and override it, it would appear on every page that includes that template in its inheritance chain.
If it's just something you want to include on every page, and it doesn't need to do any processing, you should just be able to place the code you want in the top-most template and have subsequent templates inherit that.
I typically have a "base.html" template that all of my templates inherit from. If I need something to be in every page, I put it there. If it's something I want there by default, but want to be able to augment it in subsequent templates, I will place it into a block. That block will let you include or override its default content.
I know this post is kind of old but I just came across it and found a kind-of-solution that works. I call it kind-of-solution because it is a workaround.
I have a few different sites on which I want to display logging information. This display always looks the same (it has the same html) and has the same database table and model class behind it.
My solution/workaround uses the django filters:
in views.py I put the list of log-entries in the context
context = {'list_log': Log.objects.filter(condition = True) }
template = loader.get_template('my_html_file.html')
return HttpResponse(template.render(context, request))
in my_html_file.html I use a custom filter
{{ list_log|get_log_uc|safe }}
in the filters.py I load another html file with this custom filter
#register.filter
def get_log_uc(list_log):
template = loader.get_template('user_control_log.html')
context = { 'list_log' : log }
return template.render(context)
in user_control_log.html I have the user control equivalent html
{% for log in list_log %}
<p>log.something</p>
{% endfor %

Django template system - getting fields from template

Is there a function that returns a list of fields that are expected in template? For example, I have the following template:
hello i am {{ name }}. {% for i in docs %} i have doc {{ i }}
Written in file. And i want to get a dict which contains:
{'name': 'str', 'docs': 'list'}
Is there something like that or i have to write it by myself?
As far as i know, NO....
Your template contains some html and some place holders (and may be something else). What render_to_template doing is, it gets a context dictionary which contain some keys and some data attached to that keys and a template. Then it places the values to those place holders according to their key names, execute some loops or condition checks if your template contains control flows like {% if...%} or {%for....%}
If TEMPLATE_DEBUG is closed in your settings, and if there is a place holder with no matching key in your context dictionary, then it will skip that without raising any error.
Also, if you pass a form object to your template and place your object directly to template as it is (without calling each field separately) [ex: {{form}} or {{form.as_p}} then django will check for fields on the form and palce them as it is shown here. In a such situation, you will only know that form is used. You have to check which fields are used from your Form definition.
If you look through that process, you must know what you need to place your context dictionary. You may write a parser to parse your template but it is far more difficult than just examining the template and find missing data, i guess.
The builtin {% debug %} tag may be helpful for you, however I don't know if I fully understand what you are asking. Basically, if you put the debug tag in your template it will print a lot of useful stuff, including all the variables available in your template.
Perhaps you could take a look at the source code for the debug tag (because they have access to all the variables there), and build a custom tag tag based off of this. Taking a glance at the source, this seems like it would be really simple to do.

Django - Admin - Mandatory fields with ' * '

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.