I'm trying to include a youtube like up and down voting feature for my blog. I have used pip install django-updown to install this to my project. I have followed all the instructions there.
I have included updown to INSTALLED_APPS, included rating = RatingField() to my models.py file and also included the url as described. But, the problem is, I can't see the voting system in my blog? I'm guessing it is something to do with the template file since nothing has been mentioned about it.
I have the following commenting form in my template and would like the up-down vote to be shown before this. Can anyone help me out? Thanks :)
<div class="comment_form">
{% if user.is_authenticated %}
{% get_comment_form for object as form %}
.....
{% else %}
<p>Please log in to leave a comment.</p>
{% endif %}
</div>
Maybe there is a problem with my urls.py file?
This is the url for my individual blog post:
url(r'^book/(?P<pk>\d+)$', BookDetail.as_view(), name='book-detail'),
Following the documentation mentioned in the source documentation, I included:
url(r'^book/(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)$', AddRatingFromModel(), {
'app_label': 'bookBlog',
'model': 'Book',
'field_name': 'rating',
name="book_rating"),
From django-updown github:
To submit a vote just go to video/<id>/rate/(1|-1).
If you allowed users to change they're vote, they can do it with the same url.
Try following that URL directly and check if your votes are being added or not. If it is working, i guess you could just set some links to that view or something (or send some ajax requests).
Related
How do you enable an archives tab on a pelican powered blog?
I see from the docs that it is a direct template by default, but it isn't showing up on my blog. Is there some additional field to enable it? I couldn't find any mention of it in the docs or tutorials, so I'm assuming I've missed something obvious.
Not sure if you ever resolved this; I was having the same issue as you with pelican's bootstrap3 theme. For some reason, setting a value for YEAR_ARCHIVE_SAVE_AS was not working.
After looking at the theme's base.html file, I got it working by adding the following to pelicanconf.py:
ARCHIVES_SAVE_AS = 'archives.html'
Lines 156-158 from my base.html file:
{% if ARCHIVES_SAVE_AS %}
<li><i class="fa fa-th-list"></i><span class="icon-label">{{ _('Archives') }}</span></li>
{% endif %}
I am pretty new to Django. I am fiddling with zinnia to customize it and setting it up with my own theme/template etc. The main content displayed in the default template is following:
{% for object in object_list %}
{% include object.content_template with object_content=object.html_preview continue_reading=1 %}
{% empty %}
I understand that include includes the template inside a page. But what I cannot comprehend is: how do I find the relevant template being rendered? What is content_template? Please help me in understanding this snippet.
The template name (content_template) is being fetched from the database. It is a property of the model ContentTemplateEntry and defaults to zinnia/_entry_detail.html.
I'm testing a django site on the local server: http://127.0.0.1:8000/
The sites internal links all work great, even the static links. However, when I try to link to an outside link with say google as text inside a text field of my blog model it doesnt render the link correctly. That blog model is then passed through as |safe (so that the html is rendered) to the template, and the link is instead trying to append everything to the static root:
http://127.0.0.1:8000/blog/view/http://google.com
Anyone know how to keep my static links working, but still have links that go outside of the site?
EDIT:
For example, here is a blog post that is stored in a TextField() from the admin, inside my blog app. The blog post has some links. The link to the /static/mytextfile works fine as it appends that to the http://127.0.0.1:8000/. However, the github link isnt working as it attempts to append the github link to http://127.0.0.1:8000/ and thus the outputted html creates "http://127.0.0.1:8000/http://github.com/":
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
<p><b>Example Outputs</b>
<br>a text file
Here's the 404 error that I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/view/%E2%80%9Dhttp://github.com/%E2%80%9D
EDIT 2:
This is how I have been 'escaping' the html filter. Up until now it has worked fine at leaving my <p> etc alone. It is not however leaving my href links alone!
{% autoescape off %}
{% block content %}
<p>{{ post.body|safe }}</p>
{% endblock %}
{% endautoescape %}
Something seems to be wrong with your double quote characters for the GitHub link.
Instead of:
<p><b>The Code</b><br>
<a href=”http://github.com/”>GitHub</a>
Try:
<p><b>The Code</b><br>
GitHub
I think you should try google.
I'm using django-uni-forms to display my fields, with a rather rudimentary example straight out of their book.
When I render the form fields using <form>{%csrf_tag%} {%form|as_uni_form%}</form>, everything works as expected.
However, django-uni-form Helpers allow you to generate the form tag (and other helper-related content) using the following syntax -- {% with form.helper as helper %}{% uni_form form helper%}{%endwith%} -- This creates the <form> tag for me, so there's nowhere to embed my own CSRF_token. When I try to use this syntax, the form renders perfectly, but without a CSRF token, and so submitting the form fails every time.
Does anyone have experience with this? Is there an established way to add the token? I much prefer the second syntax, for re-use reasons.
Thanks!
Have you checked the source? It should already be there. The uni_form template tag should include it automatically.
I'm having the exact same problem with django-uni-form. The csrf token does not show up after the <form> tag if I use:
{% load uni_form_tags %}
{% uni_form form helper %}
or:
{% load uni_form_tags %}
{% with form.helper as helper %}
{% uni_form form helper%}
{%endwith%}
If I include it manually it works:
<form action='{{ request.path }}' method='POST' class="uniForm">{% csrf_token %}
{{ form|safe }}
</form>
I found a blog post that outlines how to include the csrf token manually:
helper = FormHelper()
csrf_token = Hidden(
name = 'csrfmiddlewaretoken',
value = request.META['CSRF_COOKIE'])
helper.add_input(csrf_token)
Not at all pretty but at least it gets the uni-form to work.
The easiest solution is to install django-uni-form from GitHub until the version on PyPi is updated to 0.8.
pip install https://github.com/pydanny/django-uni-form/tarball/master
Use the most recent version of django-uni-form. It fixes this and so much more.
I have build a web site for a client which has a number of applications. Now he has a new URL registered which he wants to point to the same site, but he wants the look and feel changed. That's basically he wants a new home.html and base.html for the new web site. I can easily add the new site to settings and then change the view for the home page, to display a new home2.html.
However how do I do something like this as expressed in psuedo code in base.html
{% if site_id equals 1 %}
{% include "base1.html" %}
{% endif %}
{% if site_id equals 2 %}
{% include "base2.html" %}
{% endif %}
Any ideas. There are 100s of views on the site and nearly 50 models. I cannot recreate models, and mess around. This needs to be a quick fix.
Thanks in advance
You can create a context processor to automatically add site_id to the context: http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
But I would opt for a different solution. You can simply add an extra template directory per site so Django will try the templates specifically for that site first and fall back to the normal templates if they're not available.
To extend the idea of WoLph with the context processor, I would maybe even add the switching of the template to the context processor which would clean up your templates, as otherwise you may have to repeat the if clause quite often:
from django.contrib.sites.models import Site
def base_template(request):
site = Site.objects.get_current()
template = "base%s.html" % str(site.pk)
return {'BASE_TEMPLATE': template}
And in your template: {% include BASE_TEMPLATE %}
Looks nicer to me than the switching in the templates!
Another solution would be writing a Middleware to set ´request.site´ the current site id.