Conditionally including content into Django 1.2 templates - django

How can I include with Django what Symfony calls 'components' - bits of logic and a template that's not associated with the content of the current page?
For example I want to include a sidebar that displays a list of the top 10 articles on the site. It should always be displayed if the user is looking at either an 'article' page or a 'video' page. Also, the top 10 articles component needs its own CSS and JS as well as producing content.
If I have a base template that contains sections for "content", "css" and "js", and 'article' and 'video' templates that extend the base template and then define "sidebar" blocks inside "content", what's the 'Django' way of going about this?
Thanks

Custom tags are what you want - specifically, inclusion tags that let you render another template within the current one.

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 }}

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

How to add a custom template page to mezzanine?

I've been playing around with mezzanine for a couple days and I've been following this blog, which has been very helpful.
I'm now at the point where I need to make a bunch of pages that need to be based off of a custom template. My custom template is called content.html
I've put it in myProject > myApp/theme folder > templates > pages > content.html but when I look in the admin console, I don't see content in the drop down menu.
How do I get mezzanine to recognize my content.html page as a template?
content.html will not automatically appear in your site's drop down menu.
You need to go to the admin site and explicitly declare a page my content where you would like content.html to appear in your page hierarchy.
For mezzanine to match the two (i.e. template content.html and admin page my content):
Either my content's Title field (in admin site) should be content,
Or, URL field (in the meta data section of my content) should be content (if you decide the title will not be content),
Or, if you want content.html to have a custom slug, say nicecontent, then fill URL field with nicecontent and add to url.py a pattern for content.html with a matching slug, so:
url("^nicecontent/$", direct_to_template, {"template": "path/to/content.html"}, name="name_for_content").
There's a method Mezzanine uses for looking up template names, from the broadest ("page.html", which all other templates also extend), to templates named for their content types (richtextpage.html, gallery.html, etc), down to the most granular level, which is templates matching the url/slug of individual pages.
This is all covered in the documentation:
http://mezzanine.jupo.org/docs/content-architecture.html#page-templates
It sounds like you might be looking for "page.html", but it's not clear from your question.

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.