DjangoCMS render_model with HTML - django

I'm using Django CMS and the Aldryn News Blog app on a client site to display a blog listing. The default template code uses
{% render_model article "lead_in" %}
to display the "Lead in" excerpt text. However it is displaying p tags for me and I actually need them to render.
https://pasteboard.co/GSq8XObQ.png
https://pasteboard.co/GSq8ONF.png
I have tried both:
{% autoescape off %}
{% render_model article "lead_in" %}
{% endautoescape %}
{% render_model article "lead_in" |safe %}
The first does nothing and the second errors out. How can I get it to render the html tags?

Have you tried this solution?
{{ article.lead_in|safe }}

Related

flask url conflicting with href

In my HTML code i have the following jinja template code:
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
The issue is: let's assume that {{ site.site_link }} = www.domainname.com, so whenever the link is clicked it supposed to direct the user to the domainname page. but, it leads to :
http://127.0.0.1:5000/www.domainname.com
which is a 404..Any idea why is this happening and how to fix it?
I don't know if this is relevant, but, I am using flask Blueprints.
You need to put http:// in front of it, like href="http://........"
{% for site in sites %}
{{ site.site_name }}
{% endfor %}

Read a file in django and find string in file

i am a new to django, and i'm trying to execute a django code in an html template, but it is showing the same error and i dont know how to make it work. I am trying to read some files and find the one that contains the string. This is the code in the template:
{% if documents %}
<ul>
{% for document in documents %}
{% if 'TheString' in open('document.docfile.url').read() %}
<li>{{ document.docfile.name }}</li>
{% else %}
<li></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No documents available.</p>
{% endif %}
The error is the following:
TemplateSyntaxError at /subir/
Could not parse the remainder: '('document.docfile.url').read()' from 'open('document.docfile.url').read()'
What could it be, or am i doing it wrong? Thanks in advanced.
Your problem is you are trying to use Python code in the template, like your open() call.
Django templates don't work like that - you can only use a very limited set of syntax, and the syntax is not the same as Python.
Everything inside {{ }} and {% %} needs to be special Django template syntax, not Python code.
See the Django Template language documentation for the full details.
You can't use code like this in a template directly see the documentation for available template tags and filters. From what I can tell by the code you have posted in your view.py you'll need something like this:
docs=[]
for document in documents:
if 'TheString' in open('/path/to/document').read():
docs.append(document)
after passing docs to the template you could do this in the template:
{% for doc in docs %}
<li>{{ document.docfile.name }}</li>
{% empty %}
<p>No documents available.</p>
{% endfor %}
I'd need to look at what you are passing from the view to be sure of this, but I'd gladly assist in further help with further information if this doesn't help.

Django-CMS show_placeholder not working as expected

I'm working on a site where the footer content is shared across all pages. What is the best way to do in Django-CMS?
I tried using show_placeholder tag, but it somehow didn't work. A little more details on what I did:
First, I have a {% placeholder footer_info %} in base.html. Then I add a page called "Home" (template homepage.html) in django admin and put some text under footer_info as a Text plugin. As the accepted answer in this question suggested (http://stackoverflow.com/questions/3616745/how-to-render-django-cms-plugin-in-every-page),
I add
{% placeholder footer_info or %}
{% show_placeholder footer_info "Home" %}
{% endplaceholder %}
In a template called services.html which I used as the template for page Services.
However, the content in home page is not showing up in services page. I also tried adding an id home_cms_page to home page in the Advanced option area, so that I can reference it in services.html like this:
{% placeholder footer_info or %}
{% show_placeholder footer_info "home_cms_page" %}
{% endplaceholder %}
But the content is still not showing up.
Could anyone tell me what I am doing wrong? And is this the best way of getting some content from a page across all other pages (and I have to add show_placeholder in every other page)?
Thank you
EDIT:
It is not a multilingual site. I commented out 'cms.middleware.multilingual.MultilingualURLMiddleware', because the only language I use on the site is English.
I have this in my base.html:
{% load cms_tags sekizai_tags %}
<!-- all the rest of the HTML markups -->
<div class="span4">
{% placeholder footer_info %}
</div>
Then I added a page in the admin called "Home" with a Text plugin and an id of "home_cms_page".
The following is in my services.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder services_info %}
{% endblock base_content %}
{% block page_content %}
Home page
{% endblock page_content %}
{% placeholder "footer_info" or %}
{% show_placeholder "footer_info" "home_cms_page" %}
{% endplaceholder %}
Read the documentation:
If you know the exact page you are referring to, it is a good idea to
use a reverse_id (a string used to uniquely name a page) rather than a
hard-coded numeric ID in your template. For example, you might have a
help page that you want to link to or display parts of on all pages.
To do this, you would first open the help page in the admin interface
and enter an ID (such as help) under the ‘Advanced’ tab of the form.
Then you could use that reverse_id with the appropriate templatetags:
{% show_placeholder "right-column" "help" %}
I added "index" in the advanced options of the index page, and added {% show_placeholder "banner" "index" %} in the base template. It all works.

Control the form errors display while using {{ form.as_ul }} in Django templates.

I like the convenient output form method {{ form.as_ul }} but is there a way I can still continue to use it but capture all the errors upfront instead of displaying the error just above each field.
I understand that there are ways to loop through each form element and so on as mentioned in django docs but I want to continue to utilize the capability of form.as_ul() except get control over error display.
Solved this problem by using Reusable Form Templates.
Its simple...
Create a reusable template with the following code snippets based on your need.
If you want to display all errors right at the top of the form...
{% if form %}
{% if form.errors %}
{% for field in form %}
{{field.errors}}
{% endfor %}
{% endif %}
{% for field in form %}
<li>{{ field.label_tag }}: {{ field }}</li>
{% endfor %}
{% endif %}
If you want to display all errors right after each form field without the default html elements around error use...
{% for field in form %}
{{ field.label_tag }}: {{ field }}
{% for error in field.errors %}{{ error }}{% endfor %}
{% endfor %}
Used the second template and created a Inclusion Tag
The only way I see is to inherit the new form class from forms.Form and alter as_ul method as you like. Which isn't very good if you are going to use third-party forms like login form and so on (they won't have this method).
I think the best solution is to create your own inclusion tag and render form with it. It will be as short as as_ul ({% render_form form %}) but very flexible, it will work with all forms and won't mix HTML and Python code.
I still think the customization for rendering form in templates is quite flexible. I usually do this for my webapps. You may work with a bit javascript and css but not a big problem. Moreover, I think we should try to make the app simple.

How to integrate a django-disqus app into a blog

Hi my client is desperate to integrate django disqus into the blog we have built for them. I stumbled upon https://github.com/arthurk/django-disqus django disqus app and couldnt believe my luck, i had this up and running in no time, everything appears to be working ok, im posting comments etc however it dosent seem to be identifying properly as a comment posted with object.id for one blog post appears for all posts through out the blog.
in the index template that lists all the blog posts out i have
{% for entry in entries %}
{% set_disqus_identifier entry.id %}
{% set_disqus_url entry.get_absolute_url %}
{% set_disqus_developer 1 %}
{% blog stuff goes here %}
{%endfor%}
in the article template i have
{% set_disqus_identifier entry.id %}
{% set_disqus_url entry.get_absolute_url %}
{% set_disqus_developer 1 %}
<section id="comments">
View Comments
<h1>{% disqus_num_replies %}</h1>
<article class="comment">
{% disqus_dev %}
{% disqus_show_comments entry.get_absolute_url %}
the problem as i mentioned before is that if i post one comment disqus is applying that to all the blog posts. I guess im doing something wrong with the identifiers, but when i view source the javascript is getting the right id for each blog post
I really need this to work so will be eternally grateful for any help or advice that has got this working
in your index template, you don't need to do all this set_* stuff. So just load the dev tag to enable local development:
{% disqus_dev %}
{% for entry in entries %}
{% blog stuff goes here %}
{% endfor %}
In your article template just do this to display the comments. The disqus javascript will use the current URL as the identifier, so there's no need to set it manually:
{% disqus_show_comments %}
Don't forget to set the settings to the correct values as described in the documentation: http://django-disqus.readthedocs.org/en/latest/installation.html#configuring-your-django-installation And also change the url of your Site object to your actual domain.
I think you should not set the identifier and other values in index template. Because of the for loop its overriding the previous values. Rather, you should set the values in template related to particular post. That way, you would be setting disqus parameters for that particular post.
Note: django-disqus has newer version now, with django 1.7 support.