Displaying comments using django_comments - django

I have
installed django_comments
put it in INSTALLED_APPS field in settings.py
have defined SITE_ID = 1
enabled the sites framework
put url(r'^comments/', include('django_comments.urls')) in urls.py
have written {% load comments %} in home.html
But I still can't see any comments loading in my home page.
What am I missing?

{% load comments %} doesn't actually show comments...it just loads them. You need to use {% render_comment_list for [object] %} to actually show them (replacing [object] with your model name.)
Read more in the section on displaying comments in the docs.

Related

Use HTML-template from other app, in same project (Django)

I have a project which contains two apps User and Accounting. Since all of their HTML-templates should extend from the same base.html template, I made a third app called Shared, and my accounting/base.html and user/base.html would then extend from shared/base.html like
{% extends "shared/base.html" %}
{% block content %}
<div>Hello world</div>
{% endblock content %}
but that does not work, since Django looks in <app>/templates/shared/base.html.
Can this be done without having to just duplicate base.html and have the same file in Accounting and User?
You need to have all apps in INSTALLED_APPS for such template lookups.
Otherwise Django does not know that is supposed to look in templates folder inside shared app.

Additional field shows up outside table in admin change_list view

I have a model called Project in an app called projects that I registered with the admin site so the instances can be added/edited/etc. This works as expected. Now I want to add a button for each project in the change list view on the admin site, that links to a custom form that requires a Project instance to do things. I followed a bunch of different tutorials to customize the admin site and managed to add another field to the table of the change list view. However the entries show up outside the table (see image).
I added the custom field by overwriting the admin/change_list.html template and calling a custom template tag custom_result_list within it. This tag adds a table field to the change list and then calls the admin/change_list_results.html template to render it. I have confirmed with a debugger that the item is added to the entries of the change list before the template is rendered (see image).
I cannot explain why the table is not rendered correctly even though the additional field has the same structure as the auto-generated ones. I have to admit I have resorted to Cargo Cult Programming, because I do not understand how this is supposed to work, despite spending too many hours trying to solve this simple problem.
Here's the relevant code.
In file /projects/templatetags/custom_admin_tags.py:
from django import template
from django.contrib.admin.templatetags.admin_list import result_list as admin_result_list
def custom_result_list(chl):
extended_cl = {}
extended_cl.update(admin_result_list(chl))
extended_cl["result_headers"].append({
'class_attrib': r' class="column-__str__"',
'sortable': False,
'text': 'Configure Project'
})
idx = 0
snippet = '<td class="action-button">{}</td>'
for project in chl.result_list:
extended_cl["results"][idx].append(snippet.format(project.id, project.unmod_name))
idx += 1
return extended_cl
register = template.Library()
register.inclusion_tag('admin/change_list_results.html')(custom_result_list)
In file templates/admin/projects/project/change_list.html:
{% extends "admin/change_list.html" %}
{% load i18n admin_urls static admin_list %}
{% load custom_admin_tags %}
{% block result_list %}
{% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
{% custom_result_list cl %}
{% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
{% endblock %}
To fix your issue:
from django.utils.html import format_html
replace your snippet.format(...) with format_html(snippet,...)
Explanation:
in django, all strings you pass from python are automatically HTML escaped. which here means, all your tags will not be considered as HTML. Such limitation is added to avoid any potential exploits by hackers. In your case, use of a template to render html is highly recommended. However, you can also send raw html from python using format_html helper function.

How do I load an image, which was uploaded earlier ,onto a HTML file?

So I set up profile pic upload as guided by http://www.tangowithdjango.com/book/chapters/login.html.
Then I found the second answer(by user Raisins) to useful for my purpose and implemented it Extending the User model with custom fields in Django. As I've been that this answer is outdated, I've also tried the solution offered here for migration Get rid of get_profile() in a migration to Django 1.6 which hasn't improved my situation
Then in my views, I add required details including the image to a dictionary, and render i to the HTML page. It looks as though the UserProfile isn't returning the right object.
u=User.object.get(username__exact=request.user)
profile=UserProfile.objects.get(user=u)
global client
client['login']=True
client['name']=u.username
client['password']=u.password
client['email']=u.email
client['picture']=profile.picture
return render(request,'index.html',{'client':client})
And when I try to display the details on HTML page, everything except image is loaded.
I tried
<img src="profile_images/{{client.picture}}">
where profile_images is in the root directory. I inspected the element and I find this
<img src="/static/profile_images/%7B%7B%20client.picture%20%7D%7D">
where I was expecting "1.jpg" instead of "%7B%7B%20client.picture%20%7D%7D".
Any help is appreciated. Thanks
Raisin's answer is outdated. Please don't use AUTH_PROFILE_MODULE in Django 1.6 anymore. In fact, you don't even need to handle the post_save signal.
Based on the same tutorial, you will find the code for your view:
u = User.objects.get(username=request.user)
try:
p = UserProfile.objects.get(user=u)
except:
p = None
return render(request,'index.html',{'client':client, 'userprofile':p})
In your template use:
{% if userprofile.picture %}
<img src="{{ userprofile.picture.url }}" />
{% endif %}
I solved my problem by using
{% load staticfiles %}
<img src="{% static client.picture %}" />

How to add calendar events with Django-Bootstrap-Calendar?

Django-Bootstrap-Calendar (https://github.com/sandlbn/django-bootstrap-calendar) is a Django implementation of this calendar application: http://bootstrap-calendar.azurewebsites.net/
However, I can't figure out how to add events to the calendar via Django. The "non-Django" bootstrap calendar is rather simple, you just add events to the JSON file that gets loaded.
Has anyone here added events to Django-Bootstrap-Calendar before? I emailed the author of the project, however, he never responded to me.
The closest post I found to my question is here: Getting a bootstrap-calendar component to work with Django However, the author figured out how to add entries to the non-Django calendar only.
Edit: I figured out how to add events via the Admin panel.
In models.py, you simply add:
###Admin
class CalendarEventAdmin(admin.ModelAdmin):
list_display = ["title", "url", "css_class", "start", "end"]
list_filler = ["title"]
admin.site.register(CalendarEvent,CalendarEventAdmin)
Then, in the admin panel you can add events (ultimately, I want non-admin users and anonymous visitors to be able to register events on the Calendar.)
However, this reveals a larger problem: the calendar doesn't display.
Edit 2: I have the calendar loading now by adding
{% load bootstrap_calendar %}
{% bootstrap_calendar_css %}
{% bootstrap_calendar_js language="template" %}
{% bootstrap_calendar_init language="template" %}
<!-- {% bootstrap_controls 'optional-css-classes' %} -->
{% bootstrap_calendar 'optional-css-classes' %}
to my index.html file. However, the calendar doesn't display the event(s) created with the Admin Panel.
Simply put
{% bootstrap_calendar_init language="template" %}
after
{% bootstrap_calendar 'optional-css-classes' %}

How do I include a mezzanine Page with a given slug into the base template?

I am trying to include a page editable through the mezzanine admin in all the pages on my site. I read through the Mezzanine doc and the source and cannot figure out how to do this.
From the docs, I thought I could pass my page as an extra context, something like:
mezzanine.pages.views.page(request, slug, template=u'pages/page.html', extra_context={'mypage':<get_page_by_its_slug>})
But the doc says that the extra context is a mezzanine.pages.middleware.PageMiddleware object, which sets the slug from the request.
Do I need to write a context processor to do this? How do a load a specific page by its slug?
Just in case this helps someone out there, I created a context processor to solve this:
# context_processors.py
from mezzanine.pages.models import Page
def featured(request):
# editable page, get by ID or slug or title...
featured_page = Page.objects.get(id=49)
return {'featured_page': featured_page}
added the context processor into my settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
## ...etc...,
"myapp.context_processors.featured",
)
and included the featured content in the base.html template:
{% block right_panel %}
<div>
{% editable featured_page.richtextpage.content %}
{{ featured_page.richtextpage.content|richtext_filter|safe }}
{% endeditable %}
</div>
{% endblock %}
If you know of a simpler way to do this, I'd love to hear your solution!