Django: Photologue does not show images in templates - django

I am trying to install django-photologue. Everything seems ok, because I install and set up following the official guidelines. I have to upload some photos as examples. However, when viewing a photo or gallery details , then an error as follows:
Caught an exception while rendering: 'Photo' object has no attribute 'get_thumbnail_url'
I tried to remove the following code from the file photo_detail.html
{% if object.public_galleries %}
<h2>This photo is found in the following galleries:</h2>
<ol>
{% for gallery in object.public_galleries %}
<li>{%previous_in_gallery object gallery%} {{ gallery.title }} {%next_in_gallery object gallery%}</li>
{% endfor %}
</ol>
{% endif %}
No more errors, but pictures do not show up. If you click on the link will still lead to correct photographs to see. I think the problem in:
{{ object.get_display_url }}
It is totally not return any value.
Please help me solve this problem. Thanks!

Did you run python manage.py plinit after install and opt to create both a thumbnail and display photosize? These photosizes need to be defined in your database.

In other versions, you have to edit photologue/templates/photolog/tags/next_in_gallery.html and replace
{{ photo.get_thumbnail_url }}
with
{{ photo.thumbnail.url }}
Same for photologue/templates/photolog/tags/prev_in_gallery.html.

Honestly from looking at the source, it looks like a bug in the project. If you search the source, thumbnail doesn't seem to be a field within the Photo class (get_FIELD_url is an easy way to access an ImageField's url btw.) So I would recommend tinkering with the source or finding another project. I might be wrong though but that's what my ~5 minute scan of the project found.

Related

Flask-CKEditor - Error code: editor-element-conflict

In my flask app, I have 2 instances of CKEditor on 2 different files.
<div>
{{ ckeditor.create(name="message_body") }}
{{ ckeditor.load(pkg_type="standard") }}
{{ ckeditor.config(name="message_body", width=1000, height=500) }}
</div>
and
<div>
{{ ckeditor.create(name="newsletter_body") }}
{{ ckeditor.load(pkg_type="standard") }}
{{ ckeditor.config(name="newsletter_body", width=1000, height=500) }}
</div>
Each of those <div> are in a {% block main %}
I'm getting the data from from 2 different file with these lines:
request.form.get("message_body")
and
request.form.get("newsletter_body")
All seems fine, I get the data but in my browser I get this errors:
[CKEDITOR] Error code: editor-element-conflict.
Object { editorName: "message_body" }
The related docs read this:
Description: There is already an editor’s instance attached to the provided element and attaching another one to it is not allowed.
Additional data:
editorName: The name of the already attached editor.
But I don't understand why. Can any one help me get rid of these errors?
Many thanks
I just release 0.4.5 to fix this, please upgrade:
pip install -U flask-ckeditor
The reason behind this error is:
When you add a "ckeditor" class to a textarea element, CKEditor will try to initialize a CKEditor editor box on top of it. At the same time, if you initialize a CKEditor editor box manually with CKEDITOR.replace(...) (which is what the ckeditor.config() does), the error happens: you can't initialize two CKEditor editor box on one textarea element.

VSCode breaks Django template tags with newline

Problem:
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block pagetitle %}
becomes
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block
pagetitle %}
Note that the {% tag %} is being broken with a new line. This causes syntax errors with django templates.
I've tried most top django template extensions and this does not fix the issue.
I've also tried these settings:
"[html]": {
"editor.formatOnSave": false,
},
"html.format.wrapLineLength": 0,
"html.format.enable": false,
"prettier.disableLanguages": ["html"]
Desired Behavior:
Automatically format *.html files, while preserving django template tags, not breaking them up with newlines.
Sub-optimal (but acceptable) behavior: don't format *.html files at all.
I had the same issue and the only way I found that solved it is to disable the default HTML formatter. Unfortunately, I did not find a way to make it format Django template tags correctly. You can do the same if you go to VS Code Preferences > Settings > User > Extensions > HTML and uncheck 'Enable/disable default HTML formatter'.
I solved this by following this advice: https://stackoverflow.com/a/73892745/1257347
TLDR: install the djLint extension (and remember to do $ pip install djlint)
I got it to work by simply adding {{""}} between the {% tag %} that were being broken.
Example:
{% extends 'main/base.html' %} {% block title_block %}Homepage{% endblock%}
{{""}} {%block style_ref_block%}{%endblock%} {{""}} {% block body_block %}
This Didn't work for me.
The hack I found was to set the vscode language to jinja instead of the auto detected html
reference
I've also just experienced vs-code misbehaving on django template tags (i.e. deleting curly braces).
I don't like the idea of disabling HTML formatting just to support templates (i.e. vs-code Preferences/Settings/Extensions/HTML: disable (uncheck) "HTML>Format:Enable"). This is arguably a step backwards, but it does stop vs-code misbehaving.
Instead, I chose to install (vs-code Preferences/Extensions) the 'Django' extension, by Baptiste Darthenay. This was a better way to go, because it works, gracefully, preserves native vs-code HTML formatting, and includes a nice set of django snippits, which saves me keystrokes when embedding template code. Tada!
BTW, before finding Baptiste's awesome extension, I also tried keeping vs-code HTML formatting enabled, AND enabling 'HTML>Format:Templating', which promised to "Honor django and other templating language tags"; it did not.

ckeditor: Cannot set property 'dir' of undefined (django + zinnia)

I am experiencing this strange error only on my production environment. It works fine locally and on staging.
I'm using Django==1.10.5 & django-blog-zinnia==0.18.1 with zinnia-wysiwyg-ckeditor==1.3. I believe this issue happened recently when we upgraded from Django==1.8 to the latest.
When I try to create a blog entry, I can't edit the content because the ckeditor instance does not load. The errors are:
https://example.com/admin/zinnia/entry/81/change/config.js/change/ 404 (Not Found)
https://example.com/admin/zinnia/entry/81/change/lang/en.js/change/
GET https://example.com/admin/zinnia/entry/81/change/skins/moono-lisa/editor.css/change/
Uncaught TypeError: Cannot set property 'dir' of undefined
The URL is /admin/zinnia/entry/81/change/
So this very much looks like ckeditor.js is trying to load additional static files based on the current URLs and somehow it injects the filenames into the current URL.
As a workaround, I ssh-ed into the server and manipulated /static/ckeditor/ckeditor/ckeditor.8bd276b5ef4c.js and added this line at the very top:
window.CKEDITOR_BASEPATH = '/static/ckeditor/ckeditor/';
This solves the issue. The big question is: Why does this only happen on one of my machines, why does Django/zinnia not set CKEDITOR_BASEPATH correctly by itself?
I'm posting this here because I am not sure if this is an issue in Django, zinnia or zinnia-ckeditor, if anyone got insights on who is guilty, I'm happy to re-post this issue on the relevant issue tracker on github.
EDIT:
As a workaround, in my project I created the file templates/admin/change_form.html:
{% extends "admin/change_form.html" %}
{% block extrahead %}
<script>window.CKEDITOR_BASEPATH = '/static/ckeditor/ckeditor/';</script>
{{ block.super }}
{% endblock %}

Django Custom Inclusion Tags

I'm trying to build my own template tags.
I have no idea why I getting these errors. I'm following the Django doc's.
This is my app's file structure:
pollquiz/
__init__.py
show_pollquiz.html
showpollquiz.py
This is showpollquiz.py:
from django import template
from pollquiz.models import PollQuiz, Choice
register = template.Library()
#register.inclusion_tag('show_pollquiz.html')
def show_poll():
poll = Choice.objects.all()
return { 'poll' : poll }
html file:
<ul>
{% for poll in poll
<li>{{ poll.pollquiz }}</li>
{% endfor
</ul>
in my base.html file im am including like this
{% load showpollquiz %}
and
{% poll_quiz %}
Bu then I get the the error:
Exception Value: Caught an exception while rendering: show_pollquiz.html
I have no idea why this happens. Any ideas? Please keep in mind I'm still new at Django
Shouldn't all custom filters be inside the templatetags directory?
templatetags/
__init__.py
showpollquiz.py
then
#register.inclusion_tag('show_pollquiz.html')
looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template
You forgot to close your template tags... Also, you should change the name in the for tag, you can't have for poll in poll:
<ul>
{% for p in poll %} <!--here-->
<li>{{ p.pollquiz }}</li>
{% endfor %} <!--and here-->
</ul>
Also notice you're not using the inclusion tag you defined at all. I think you got some code mixed up, try to go over a tutorial start to end and things will be clearer.
I'd not bother with writing your own template tags: take things one step at a time and stick to the basics for now. There's nothing wrong with {% include 'show_pollquiz.html' %}
I found the problem. The problem was that the #register.inclusion_tag('show_pollquiz.html')
inclusion tag is obviously looking for the template in the default_template directory. So that is why i got the error.
According to me this is not clear in the documentation. But I guess its how it is, being a template and all...
Oh , well.
Now, how would I put the #register.inclusion_tag('show_pollquiz.html') to look in the same folder as the app? under templatetags/?

Create a PDF or Printable version of change_list.html in Django Admin

I would like to add a tool link at the top of my admin change_list.html, which I have already done, and have this link basically be able to produce some sort of printable document version of my models data based off of my current filter settings. Basically a print button in the admin change_list.html.
so far I have overridden the change_list.html to create the link, and I notice that this
<li>
<a href="{{ choice.query_string|iriencode }}" class="addlink">
{% blocktrans %}View PDF{% endblocktrans %}
</a>
</li>
gives you a link based on these choices.. but Im kinda lost as to the best/easiest way to do this..
Sorry, new to Django. I know I can use ReportLabs to generate pdfs, but not a 100% on how to get the filtered data from change_list to it.
A bit late, but for those who might be searching "in the future" like me, this might be helpful: http://djangosnippets.org/snippets/1842/