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.
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 created a variable in my python script. I want to display the value of this variable on my page. So I passed the variable to a dictionary and call the key on my HTML file. However, when I refresh my page it does not show any error and yet the value too does not show.
I am new to python, I do not know what the problem could be. Please is there any way to enable this template tag {{ }} before use?
Like so: Python function
from django.shortcuts import render
def about(request):
my_name = 'Hello! My name is Andi#ITech'
return render(request, 'about.html', {"my_name":my_name})
HTML File
{% extends 'base.html' %}
{% block title %} About Andi#ITech {% endblock %}
{% block content%}
Home | About
<br><br>
<em>About Me</em>
<br>
{% if 2 > 21 %}
Halo!
{% else %}
GOODBYE
{% endif %}
<br>
{{ my_name }} <-- this line of code does not show anything -->
{% endblock %}
I expected the following sentence to display on my page
: Hello! My name is Andi#ITech.
The syntax looks good to me. Are you sure your block is executing correctly? It is enough that there is typo in the name of the base block (like contetn) and the block in the about.html file is not executed. It is also possible that the block is correctly executed but there is an error in the HTML and the browser does not show the text because it is inside a tag. Did you check the HTML resulting code? Also CSS can hide your text... It is not easy to give you a precise answer without all the code.
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 writing a django template Configuration_Detail.html and it renders correctly on the relevant urls. It does not, however, take any variables whatsoever from the view class. I had a very similar template Configuration_List.html which worked fine, though that was a ListView not a DetailView.
Configuration_Detail.html:
{% extends "base.html" %}
{% load i18n %}
{% block title %}{% trans 'MySite Database' %}{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'MySite Database: Current Instrumentation Configuration' %}</h1>
{% endblock %}
{% block content %}
Here is some text {{name}} with a variable in the middle.
{% endblock %}
The page renders the title bar fine, but the content block becomes "Here is some text with a variable in the middle."
I believe it should be taking the variable {{ name }} from here.
views.py:
class ConfigurationDetail(DetailView):
model = Configuration
def getname(self):
name = 'debug'
return name
But it does not...
Any suggestions on how to fix this would be greatly appreciated.
Edited to add:
Models.py - Configuration:
class Configuration(models.Model):
title = models.CharField(max_length=100,unique=True,blank=False)
author = models.ForeignKey(User)
created = models.DateField("date created",auto_now_add=True)
modified = models.DateField("date modified",auto_now=True)
description = models.CharField(max_length=512)
drawing = models.ForeignKey(Drawing,blank=True,null=True)
instruments = models.ManyToManyField(Instrument)
def __unicode__(self):
return self.title
The get_context_data() method is using ctx['author'] = Configuration.author
For DetailView, an object variable is added in the context which points to the database object for which the view is being rendered. So in your template you can do:
{% block content %}
Here is some text {{ object.author.get_full_name }}
with a variable in the middle.
{% endblock %}
The get_full_name method is from the User object.
if i understood correctly you need to access a model property from within a template, but for that is sufficient to do {{ configuration.author }} without modifying the context data at all!
the DetailView puts in the context the selected model, accessible with the dot notation.
I need a subnavigation on my pages. I inherit from base.html where the main navigation is located, but I don't know how to make a subnavigation which differs from page to page.
I've thought about making a template tag in which I can specify items to the subnavigation in each template file, and only output the subnavigation if any subnavigation items are specified. How do others do it?
Why can't you have a separate block for subnavigation and override that block in child templates?
base.html
Calls
Messages
{% block subnav %}
{% endblock %}
calls.html
{% extends "base.html" %}
{% block subnav %}
Outbound calls
Inbound calls
{% endblock %}
messages.html
{% extends "base.html" %}
{% block subnav %}
Sent messages
Recieved messages
{% endblock %}
A simple way to implement such could be to create a templatetag that takes an argument, for example the unique id or slug of the current page in the page tree.
E.g.
#register.simple_tag
def subnavigation_for_page(request, page_id, *args, **kwargs):
qs = Page.objects.filter(is_active=True)
current_page = qs.get(id=page_id)
sub_navigation = list()
for page in qs.filter(level__gt=current_page.level):
if page.level > current_page.level:
sub_navigation.append(page)
return sub_navigation
You could extend the example to return the rendered navigation as html or use the 'as' node to return a variable and define the html within the template itself.
Package that might be of interest:
django-mptt