For starters, i'm new in Django.
I have a searchbar and i want to show it on only single page. I use {%block searchbar %} {%endblock%} on all pages where I don't want to see the search bar. But suddenly I thought: I'm duplicating the code, and this violates the principle of "DRY". So, how can I display some content on a single page without duplicating this {%block searchbar %} {%endblock%} stuff?
In advance, thanks for your help!
Use include tag to include the search bar on the pages that you want. See here for more from the docs.
Place the code for the search bar in an HTML file called "searchbar.html
and then include it in any pages that you want.
{% extends "header.html" %}
{% block bar %}
{% include "searchbar.html" %} <!-- Simply include this on pages that you want -->
{% endblock %}
this way you will not violate the DRY principle.
Related
For my project, I'm searching for articles on google news based on keyword input by the user, I want to display these links obtained from the search on my results page.
this is my result.html
{% extends 'base.html' %}
{% block title %}Result{% endblock %}
{% block content %}
<h3>Reported Url</h3>
<div>Post Content: <br>{{content}}</div>
<h3>News articles related to your query:</h3>
<ul>
{% for key, value in articles.items %}
<li>{{value}}</li>
{% endfor %}
</ul>
<div>
Back to Home Page
</div>
{% endblock %}
But the links do not work and I get the page not found error, since these links are not contained in urls.py.
How can I link these urls correctly?
thank you
You cannot execute python code inside Django templates.
Check out this thread: Numeric for loop in Django templates
Fixed the problem I had to replace "./" in the article to the homepage of the site I was refering to.
I'm trying to add a custom Title to my product pages using the block title tag in Django.
I have a view function to return data to a template for the product page:
def deal_by_detail(request, slug):
deal_detail = Deal.objects.filter(slug=slug)
return render(request, 'deals/deal_detail.html', {'deal_detail': deal_detail})
and URL for the deal_detail page:
url(r'^(?P<slug>.*)/$', deal_by_detail, name='deal_detail'),
In my 'deal_detail.html' page I have successfully displayed all the information from a particular product... like so:
{% for deal in deal_detail%}
{{ deal.title}}
{{ deal.price}}
{% endfor %}
However i'm having an issue with the block title. Since it comes before the aforementioned loop, i realize I can't reference the title like so:
{% block title %}{{deal.title}}{% endblock %}
I've also tried it like this:
{% block title %}{{deal_detail.title}}{% endblock %}
But that doesn't work either --nor does just {{ title }}
I also experimented with a duplicate of the other loop and that doesn't work either.
Here is what I have on the base template page that is extended from this deal_detail template:
<title>My Site - {% block title %}{% endblock title %}</title>
Just kind of stumped and not sure if I need a class based view or something I'm totally missing here. Thanks in advance.
I am trying to set up a proper workflow for a personal website.
I am using the Cactus static site generator which makes use of the Django template system.
I know what I want to do, but I am not sure how to do it as the tutorials for Cactus are limited.
This is what my directory structure looks like:
/mysite/pages/
/mysite/templates/
/mysite/mycontent/
/mysite/plugins/
My template, /mysite/pages/menu.html, looks like this:
<p>Welcome to the page!</p>
{% block body %}
{% endblock %}
And one of my page articles, /mysite/pages/testpage.html, looks like this
{% extends "menu.html" %}
{%block body %}
<p> Test Content </p>
{% endblock %}
But what I am trying to do is set this up so that whatever I want to write for Test Content can be written somewhere else and the copied in to the folder. Something like this:
{% extends "menu.html" %}
{%block body %}
{%include "../mysite/mycontent/TestContent.html}
{% endblock %}
Is this something that Django templates needs to manage? Like I said, I am using Cactus which uses Django templates but I have looked around and am not sure what the standard way of doing this is, even though it seems to work with MVC/MVT philosophy.
There is also an option to use Django plugins with Cactus.
Thanks!
I figured out what I was trying to do. I just had to include the html I wanted using the template language.
{% extends "menu.html" %}
{%block body %}
{% include "./file.html" %}
{% endblock %}
I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template.
How should I structure the code in views.py?
The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right?
You can do:
<div class="basic">
{% include "main/includes/subtemplate.html" %}
</div>
where subtemplate.html is another Django template. In this subtemplate.html you can put the HTML that would be obtained with Ajax.
You can also include the template multiple times:
<div class="basic">
{% for item in items %}
{% include "main/includes/subtemplate.html" %}
{% endfor %}
</div>
You can do this using a block. Blocks are a Django Template tag which will override sections of a template you extend. I've included an example below.
basic_template.html
<body>
{% block 'body' %}
{% endblock %}
</body>
template you want to include: (i.e. example.html)
{% extends 'basic_template.html' %}
{% block 'body' %}
/* HTML goes here */
{% endblock %}
views.py:
return render_to_response(template='example.html', context, context_instance)
Doing this will load basic_template.html, but replace everything inside of {% block 'body' %} {% endblock %} in basic_template.html with whatever is contained within {% block 'body' %} {% endblock %}.
You can read more about blocks and template inheritance in the Django Docs
There are mainly 2 ways (2 easy ones)
1:
In base html put
{% include "myapp/sub.html" %}
And just write html code inside your sub.html file
2:
https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance
I just wanted to add differences of extend and include.
Both template and include can use models inserted in current app.
Template is for global usage by your any app. Include is for use in certain apps.
For ex: you want to insert Image Slider to your homepage and about page but nowhere else. You can create Slider app with its own model for convenience and import its model and include in that pages.
If you used template for this example, you would create 2 templates one with slider and everything else other template have.
I am trying to create a single page with different content from different template in django so i can print it. Its kind of summary of different page
base.html - mainContent block is rendered inside this template
main.html - Need mainContent block from here
graph.html - Need mainContent block from here
charts.html - Need mainContent block from here
summary.html - Need content from main, graph, charts all together here (REQUIRE)
I have a base template which is extended on every page like this (It has navbar, footer and sidebar)
{% extends "base.html" %}
There is a block inside base template where graph, main, chart content is displayed. What i am trying to accomplish is to get the mainContent from those page and add it to new template called summary.html
Since every page is extending from base i am not sure how to do that? I tried using include but it will also include base for every page.
EDIT: I know i can separate the mainContent into its own separate files but i have lot of templates and was looking for any other solutions.
You could separate the content.
I guess you have something like this:
<!-- graph.html -->
{% extends "base.html" %}
{% block 'mainContent'%}
{# Your graphs html here #}
{% endblock %}
But you could put the graph html in a separate, let's say graph_template.html template and include it:
<!-- graph.html -->
{% extends "base.html" %}
{% block 'mainContent'%}
{% include 'graph_template.html' %}
{% endblock %}
And then in summary.html you can include graph_template.html.