django python ajax java script html - django

Hello every one , my problem is in django and ajax
i want to use two block one for django and the other for ajax but the ajax code is not reading , why ?
{% extends 'store/main.html' %}
{% load static %}
{% block content %}
// code html
{% endblock content_ajax %}`
block content_ajax your text%}
//code ajax
{% endblock content_ajax %}`
#

Inside store/main.html
{% block content_ajax %}
{% endblock content_ajax %}

Related

Reuse same "block" of html in multiple django templates

Currently, I have two html template that extends from a base.html:
page1.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
Code_block_1
{% endblock %}
page2.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
Code_block_1
{% endblock %}
Both html share the same Code_block_1.
I was thinking about about creating another html called Code_block_1.html to consolidate this repeating piece of code. Then, insert Code_block_1.html into page1.html and pag2.html. Django only lets you extend once. How do I get around this problem?
Thanks.
Simply create another HTML file called code_block_1.html and then inside both page1.html and page2.html use include like this:
<!-- page1.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}
<!-- page2.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}

How to access template block from plugin within Django CMS Placeholder

I have the following code:
base.html
<html>
...
{% block test_block %}
{# Some stuff to render #}
{% endblock %}
...
</html>
main_template.html
{% extends "base.html" %}
...
{% block content %}
{% placeholder "content" %}
{% endblock %}
...
plugin.html
...
If I add my plugin to the page it renders in the placeholder block, as expected.
If I amend plugin.html to add the following:
plugin.html
...
{% block test_block %}
{{ block.super }}
{# Some more stuff to render #}
{% endblock %}
...
Then I receive an error:
'BlockNode' object has no attribute 'context'. Did you use {{ block.super }} in a base template?
If I try and extend either main_template.html or CMS_TEMPLATE (they seem to be the same?) from within plugin.html I get the following error:
maximum recursion depth exceeded while calling a Python object
How can I access and append to test_block from within my plugin?
In order to use a block you must, I believe, do so in a template which extends from a template where that block is defined.
However, to do what you want, CMS makes use of django-sekizai.
With this you can have this in base.html;
{% load sekizai_tags %}
<html>
...
{% render_block "test_block" %}
...
</html>
And then in plugin.html you can do;
{% load sekizai_tags %}
{% addtoblock "test_block" %}
Add this to my block
{% endaddtoblock %}
This is often used for CSS & JS blocks, but works perfectly well for what you want to do.

No content visible in django-bootstrap3

I am using Django 1.9 and demo template structure to display home.html.
I have included the following in the base.html file:
{% block bootstrap3_content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
...
</div>
</nav>
<div class="container-fluid">
{% autoescape off %}{% bootstrap_messages %}{% endautoescape %}
{% block content %}(no content){% endblock %}
</div>
{% endblock %}
Navbar shows successfully but I do not get the Bootstrap message in the container below?
I am a new user to Django and this App as well. Please help!
Edit1:
bootstrap.html extends bootstrap3.html; base.html extends bootstrap.html; home.html extends base.html.
home.html
{% block title %}My_title{% endblock %}
{% block content %} This is <em>bootstrap3</em> for <strong>Django</strong>.
{% endblock %}
I get the title in the navbar but no message inside content block.
Make sure that you have
{% load bootstrap3 %}
at the top of your page since you are using this module. Make sure that you have
'bootstrap3',
in your installed apps.
From your code it looks like you are extending a template. If you are, be sure to have a
{% extends FooTemplate.html %}
in case you are loading boostrap3 in the parent template.

Compositional Templates Django

What is the best way to create modular templates?
For example, if I have something like this:
#base file:
{header}
{block content}
{footer}
#main file:
{extends base}
{block content definition}
#product file:
{extends base}
{block content definition}
This is the django way from what I understand - filling in the blanks.
Now, my question is what if I needed a page like this:
{header}
{main}
{product}
{footer}
Basically, the main, and product are both inside the base file. I could try a deep inherit (a page that has main, which then consecutively loads product) but that does not work since the files both define 'content', and that is not possible in Django
You need do two templates "_main.html" and "_product.html" which don't extend base, just their own content.
After that you can use them via include tag https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
#main.html file:
{% extends 'base.html' %}
{% block content %}
{% include '_main.html' %}
{% endblock %}
#product.html file:
{% extends 'base.html' %}
{% block content %}
{% include '_product.html' %}
{% endblock %}
#and what you want:
{% extends 'base.html' %}
{% block content %}
{% include '_main.html' %}
{% include '_product.html' %}
{% endblock %}
{% block footer %}
{% endblock %}

Use django-pagination to generate link refl=next/prev

I'm using django-pagination to paginate my pages. It works great, but I would like to set up
<link rel="prev" href="http://www.example.com/foo/?page=1" />
<link rel="next" href="http://www.example.com/foo/?page=3" />
to the <head>, like it is recommended by google .
However I found no way ho to do this (without extra queries at least). First I tried to edit the pagination/templates/pagination.html with something like this
{% block extra_head %}
<link rel=... ... />
{% endblock %}
Which of course did not work (pagination.html is included by the {% paginate %} tag, it does not extend my layout.html). Next, I tried to modify my template for /foo/ view to something like this (adding the {% block extra_head %}):
{# foo.html #}
{% extends "layout.html" %}
{% block content %}
{% load pagination_tags %}
{% autopaginate object_list %}
{% paginate %}
{% for obj in object_list %}
{{ obj }}
{% endfor %}
{% paginate %}
{% endblock %}
{% block extra_head %}
<link rel="prev" href="?page={{ page_obj.previous_page_number }}"/>
{% endblock %}
But this won't work either, as the page_obj variable is only available in scope of {% block content %}. A could call
{% autopaginate object_list %}
in the extra_head block, but that will mean an extra hit to the db (and possibly other side effects that I'm not aware of). Is there an elegant way to solve this, ideally as DRY as possible?
Edit: I'm using django 1.2.
You can do {% autopaginate %} in higher-level block, then paginated objects will be available in sub-blocks. If you don't have higher level block it is possible to do this in base template:
{% block root %}
...
{% endblock %}
And in extended template:
{% extends "base.html" %}
{% load pagination_tags %}
{% block root %}
{% autopaginate objects 10 %}
{{ block.super }}
{% endblock %}
<!-- the rest of your code -->
Now, to get a different rendering of paginator in head, you can make use of the with tag:
{% with we_are_in_head=1 %}
{% paginate %}
{% endwith %}
And override templates/pagination/pagination.html with something like this:
{% if we_are_in_head %}
# Your links to next and prev only
{% else %}
# original code
{% endif %}
A moral
This is not elegant and the reason is that pagination should be implemented in the view. Templates are for rendering only, template-tags too. Pagination makes extra sql queries, it also parses arguments from request, template is totally wrong place for this code, so workarounds has to be invented. These workarounds might break on next release of django, they are also subtle and can be accidentally broken by other developer.
We can call autopaginate in a view and then use {% paginate %} as usual. Here is a recipe if somebody still face the described problem:
from pagination.templatetags.pagination_tags import AutoPaginateNode
def autopaginate(request, context, queryset_var, paginate_by):
""" It allows us to use paginated objects in different template blocks """
autopagination = AutoPaginateNode(queryset_var, paginate_by)
# Inject a request - it's required by `autopagination` function
context['request'] = request
autopagination.render(context)