How to get mezzanine sidebar to autopopulate for blogposts - python

I have a blog running mezzanine and I can't get the right side bar to auto populate after a blog post with the information about the blog post, like author, tags, etc. The only way I can get it to show at all it to manually edit the base.html file and add a widget for each field. Isn't there a way to automate this?
Thanks again.

By default, mezzanine includes something similar to what you're asking, the filter_panel.html template. This is the template the blog app uses. In whatever template you're using, include this template in the right_panel block, eg:
{% block right_panel %}
{% include "blog/includes/filter_panel.html" %}
{% endblock %}
If you're just using base.html, you can override the right_panel block by replacing it with just the above includes tag.
Hope that helps!

Related

Logo doesn't display using Django admin_interface

I have django-admin-interface installed in my django app. The problem is that when I try to upload a logo it says that all is okay but the logo doesn't show.
Logo uploaded correctly but not showing
I want to change the default Django logo.
Is there any way to accomplish this?
Create a new template and extends the 'admin' template like below to override the default admin template.
{% extends "admin/base.html" %}
{% block branding %}
<h1 id="site-name">MY_LOGO_OR_TEXT</h1>
{% endblock %}
In the code above, change the MY_LOGO_OR_TEXT section with your html logo or just text to be shown on the admin panel like you wanted.
ADMIN

Django multiple templates

I'm building a site where users can view their posts Like this. After building the quizzes portion, I tabbed to "blogs" where I realized I needed to import the blogs template to use it.
I'm using the quiz template already like this
{% extends '../main/base.html' %} {% block title %}View Quizzes{% endblock %} {% block content %}
but I need to access the blog template as well. How can I do this? Thanks!
You can include the blog template into the main one like:
{% include "path/to/blogs.html" %}
It's best to have a main template which includes blogs, quizzes... (instead of inheritance)
You don't need django to do this.

In Django how to render hello.html in base.html?

I am new to django and I misunderstand how to use templates.
I have a a file called base.html which I see as a parent to hello.html.
In hello.html I have this syntax:
{% extends "base.html" %}
{% block hello %}
<h1>hello</h1>
I should see this template. This is the hello.html template.
{% endblock %}
In base.html I have this syntax:
{% block hello %}{% endblock %}
It is my understanding that django should render hello.html inside of base.html
When I deploy my two html files, django ignores my syntax.
Question: How to render hello.html in base.html?
The files are visible inside of github:
https://github.com/danbikle/sof1231/blob/master/hello/templates/base.html
https://github.com/danbikle/sof1231/blob/master/hello/templates/hello.html
Also I deployed them to heroku with these commands:
heroku create sof1231
git push heroku master
You can see base.html deployed to https://sof1231.herokuapp.com
Again,
How to render hello.html in base.html?
To render a template in another template, you use include:
base.html
{% include 'hello.html' %}
Your templates are designed to work with inheritance, and there is nothing wrong with the simplified templates that you show in your question (I didn't check those on github).
I think that your problem might be caused by your view rendering the base.html template, when it should instead be rendering the hello.html template. You should add your view code to your question so that this can be verified. Your view code should be something like this, which renders the child template hello.html:
def hello(request):
template_variables = {'a': 1, 'b': 2}
return render(request, 'hello.html', template_variables)
Another answer (which you have accepted) recommends using include. I don't think that include is the correct approach.
There is a difference between inheriting from a base template and simple inclusion of content from another file. One important benefit of template inheritance is that you can add common content (e.g. menu, side bars, footers, etc.) to a "base" template and then inherit from that base in child templates without duplicating the common content for each page. Another benefit is that the child templates can override content in the base templates, e.g. <title>. This allows you to markup areas of your layout in the base template (using block) and then override the content of the block with other content. This is not possible with a simple include.

Creating a "Recent Posts" list in a sidebar.

I'm working on a simple blog app in Django, and i'm having trouble figuring out how to dynamically generate the five most recent posts in a side bar. Each of my views are class based and they extend a generic template, each view maps to one template which I believe is the correct way to do it. I've looked for a way to do this using template tags, but it seems Django doesn't like you to put any logic inside of your templates.
The problem I believe is that I want this to exist within my base.html because I want the recent posts to be displayed site-wide, is a view even supposed to map to your base.html or does that cause problems, i'm pretty new with this. I don't know how to approach this, whether i'm supposed to create a new view for base.html or if I should use my template tags, or if I should extend an existing view(but if I do that it won't be site wide?).
I essentially want the following(they're ordered in reverse chronological order)
{% for post in post_list[:4] %}
{{ post.title }}
{% endfor %}
You can use a template tag. More specifically, an inclusion tag is what you need. This allows you to insert a rendered snippet anywhere inside your template via a small view-like piece of code.
For example, create a templatetags/blog_tags.py file (it's important that you create the templatetags folder within your app; Django searches for them here by default) in your blog app and add the following:
from django import template
register = template.Library()
#register.inclusion_tag('blog/snippets/recent_posts.html')
def render_recent_blogposts():
return {
# This is just an example query, your actual models may vary
'post_list': BlogPost.objects.all().order_by("published_on")[:4]
}
now create a blog/snippets/recent_posts.html template (it can be anywhere as long as it mathecs the #register.inclusion_tag(...) above.):
<ul>
{% for post in post_list %}
<li> {{ post.title }}</li>
...
{% endfor %}
</ul>
finally, in your original template, you can now render your template tags:
<aside>
{% load blog_tags %}
{% render_recent_blogposts %}
</aside>

separate member and guest template content in django

I'm trying to figure out what is the best way to have mixes member and guest templates.
The main difference would be the menu of the page. In some languages i've worked with you can add prefixes to templates to get it to switch out the whole templates for the other version.
ex:
base.guest.html
base.member.html
In Django the only way i've seen any thing related to this is this code i found in the documents:
if request.user.is_authenticated():
# Do something for authenticated users.
else:
# Do something for anonymous users.
Is this the base way to do this in Django? or is there something else that i'm missing.
For most of my pages this would work out ok but wasn't sure if there was a better way to switch content based on authenticated state.
You don't extend in this case, instead you include. The base.html should authentication-agnostic.
{% if user.is_authenticated %}
{% include 'member.html' %}
{% else %}
{% include 'guest.html' %}
{% endif %}
If you want to, you can do have the if-statement also in your view, and pass the name of the template-to-be-included to the main template. For more info see https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

Categories