How do I access the `BLOG_SLUG` string? - python

When I type {{ settings }} on a template and open it with Mezzanine, it shows me {u'MEZZANINE_ADMIN_PREFIX': u'grappelli/'}. I'm trying to access settings.BLOG_SLUG, but cannot get that setting to appear in the template. Here's a small snip of what my template looks like.
{% load mezzanine_tags keyword_tags i18n %}
{% block main %}
{{ settings }}
{% endblock %}
How do I get my template to display the string stored in setting.BLOG_SLUG?

you have to pass settings.BLOG_SLUG from your view than you would be able to display it in html.
for example you have written line settings.py file like
BLOG_SLUG = 'something'
than you can retrieve it in your view as
SETTINGS.BLOG_SLUG
and you can pass it from your view to html

Related

How can I apply base.html file for extending to every template in django

Is there a way to extend base.html file in python django by default, I want it because I am overwriting {% extends %} tag in every html file
No but what you can do however is created a new HTML file called for example, header-tags.html with something like that :
{% extends "base.html" %}
{% load static %}
.... more tags
And then include this snippet wherever you want with {% include 'header-tags.html' %}

How to fix Python template tags for calling variables in HTML page, like {{ my_name }}

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.

Django: Is it possible to call a template from inside another template? [duplicate]

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.

Including Django templates in an arbitrary order

I'm using separate template files to create "widgets" - HTML snippets that are displayed in a grid layout on the page. The main template includes the widget templates like this:
{% include "myapp/widgetA.html" %}
{% include "myapp/widgetB.html" %}
{% include "myapp/widgetC.html" %}
{% include "myapp/widgetD.html" %}
This works fine, but I want the user to be able to change the order that the widgets appear in. I will have the user's preferred ordering stored in a tuple, eg: ('widgetC', 'widgetB', 'widgetA', 'widgetD')
How can I handle this in the template?
You can iterate over the tuple in the template:
{% for widget in my_widgets %}
{% include widget %}
{% endfor %}
where my_widgets = ('myapp/widgetC.html', 'myapp/widgetB.html', 'myapp/widgetA.html', 'myapp/widgetD.html') in your view.

How do I access my own template variables from within a custom template tags template?

I am using Django to build a website.
I have a context processor setup that looks something like this:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"mysite.context_processors.mystandardvariables"
)
This adds some standard variables that I like to use in templates, such as SITE_NAME and SITE_ROOT.
I have just created my first custom template tag and I find that I cannot access these standard variables.
I don't get any errors and my page displays ok, it's just that the variable that I want are not available.
To check which variables are available I already used {% debug %}.
My tag looks like this:
#register.inclusion_tag('search/search_snippet.html', takes_context = True)
def search(context):
form = forms.SearchForm()
return {'form': form }
The template for the tag looks like this:
<form action="{{ SITE_ROOT }}search" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
I am including the search tag in my home page like this:
{% extends "base.html" %}
{% load search_tags %}
{% block content %}
{% search %}
{% endblock %}
To answer my own question, I figured out a way to do what I want using a normal template tag rather than an inclusion tag.
#register.tag
def search(parser, token):
return SearchNode()
class SearchNode(template.Node):
def render(self, context):
return render_to_string('search/search_snippet.html',
{ 'form' : forms.FindForm() }, context)
Here I am passing the context through to the function that renders my template to a string.
I would have preferred to implement this as an inclusion tag as it seems like less work, but I wasn't sure how to get it to work.
If anyone knows how to get this working with an inclusion tag please answer and I'll mark your question as the right answer.

Categories

Resources