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

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.

Related

How to get mezzanine sidebar to autopopulate for blogposts

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!

How to use templates and connect all components in Django?

I'm new to Django and I'm having a somewhat hard time understanding how to connect all of the different pieces together. All of the tutorials I've read on Django templates don't explain how to connect all of the pieces.
I have created my base template called base.html. I have a couple functions inside my views.py class that do specific things. Now I want to create pages that inherit from base.html and display information with respect to each function. So say I want action1.html to call the action_one function and action2.html to call the action_two function. I don't really get how to do this. Any help would be appreciated.
sorry for pasting image but this can help you understand how the flow goes:
there are lots of things going on but i didnot draw them so you can see how the basic flow goes.
for the red part, you can use render_to_response as Thomas says. but i would use render as Kevin does.
here the difference:
render() is the same as a call to render_to_response() with a
context_instance argument that forces the use of a RequestContext.
hope this helps a bit
You're misunderstanding how Django templates and views interact: in Django, views render templates, not the other way around (i.e. templates do not call views).
One example of a view rendering a template is the render_to_response helper function.
As for what defines what view is called when a given URL is accessed, this is your URL configuration.
The following is probably where you want to start here:
In your URL configuration, map the /action_one/ URL to the action_one view.
In your action_one view, render the action_one.html template
Hopefully this code example helps lay it out...
urls.py
url(r'^action1/$', 'yourapp.views.action1'),
url(r'^action2/$', 'yourapp.views.action2'),
views.py
def action1(request):
return render(request, 'action1.html')
def action2(request):
return render(request, 'action2.html')
base.html
<html>
...stuff...
<body>
{% block action %}
{% endblock action %}
</body>
</html>
action1.html
{% extends base.html %}
{% block action %}
... action1 html stuff ...
{% endblock action %}
action2.html
{% extends base.html %}
{% block action %}
... action2 html stuff ...
{% endblock action %}

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>

Add csrf_token to base.html template

In my django project, I'd like to add the csrf_token template tag to the base.html template.
Just dropping the template tag in doesn't get it populated, I'd have to add it to every view, which is not optimal.
So, is there a way to add the csrf_token on every page?
I would think this would be built into whatever view renders the base.html template.
Though I don't know why it's not rendered, it should be inside a form element for it to work properly.
Create a form.html template and reuse it with :
{% include 'form.html' with form=contact_form %}
Place the csrf tag inside form.html

Django: Can't override admin templates when they are already overridden?

To add some content to a Django admin view of a model, I want to override the change_form.html template. According to the documentation I would need to create a file change_form.html in a the folder /project-path/templates/admin/appname/modelname/. Of course I need to make sure, that this path is also available in TEMPLATE_DIRS. Such a file could look like this:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block after_field_sets %}
SOME CONTENT
{% endblock %}
However, I make use of django-guardian to have object permissions. This Django app overrides change_form.html as well (which works fine -- relevant source seems to be here), but Django doesn't pick up my template extension file (i.e. "SOME CONTENT" from the sample above isn't displayed). The blocks/parts I want to override are not the same ones that django-guardian overrides and eventually I want to have the additions to change_form.html of django-guardion and of my template.
What am I doing wrong here? And is possible at all to have multiple applications overriding an admin template?
If it is of interest, this is my TEMPLATE_LOADERS setting:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
)
Also, django-guardian is the last app in the INSTALLED_APPS array.
One possible solution seems to be to explicitly define the inheritance chain by referring to and overriding django-guardian's template (defined here) and not Django's general change_form.html. So instead of using
{% extends "admin/change_form.html" %}
in the beginning of my custom template, I would need to use
{% extends "admin/guardian/model/change_form.html" %}
Also I would need to extend my GuardedModelAdmin sub-class model to explicitly use my own template file as the change form template:
class MyModel(GuardedModelAdmin):
change_form_template = 'admin/appname/mymodel/change_form.html'
This works, but it adds a clear dependency to the template and the model. Of course, the model has this dependency anyway, but I would be interested if there is also a solution that refers only to the default change_form.html -- however, I suspect that this is not really possible.
If a path to your template is in TEMPLATE_DIRS, then you can reverse order of your TEMPLATE_LOADERS.
If your template in in your app, then you need to add your app after django-guardian in INSTALLED_APPS array.
Basically, if there are name collisions, then last loaded template will be in use.

Categories