I have created a site mainly using django's admin interface, plus a few custom views. As the majority of the site is using the admin (and I am not to hot with css), I have just used django's admin tempates for my custom views (they are extended generic views).
Anyway, most of my custom views look good, and match the look and feel of the admin interface, but I don't know how to get the breadcrumbs working.
So form an extended generic view, how and what do I pass to the tempate's
{% block breadcrumb %}
tag?
I saw one article that mentioned the context object, but didn't have any further details.
If you want to provide breadcrumbs in your template and get breadcrumbs from parent template you can use block breadcrumbs & block.super variable in it:
{% block breadcrumbs %}{{ block.super }} › My custom site{% endblock %}
Or just pass to the template variable title.
Related
I have created a stream block with a custom template in my models.py file for my 'work' page as:
section = StreamBlock( [ ('section', SectionStreamBlock( template = 'personal_web/blocks/work_block.html') ])
I am listing my 'work page' objects in a 'work-index page'. As work-index page > work page > block
Now I am trying to access the 'work page object' itself in my block template. (I am using jinja2 )
I know that I can not pass it via {% include_block block %}.
Is there a way to pass it?
The {% include_block %} tag works in the similar way to the {% include %}tag from Jinja2 in terms of passing context to a child template. So if you render the section streamfield in your page template like this:
{% include_block page.section %}
In your personal_web/blocks/work_block.html you should be able to access your "work" page object using the page variable (for example, page.title to access page's title).
I tried searching but could not find any answer to this, in Django crispy forms there is an HTML layout Object which allows you to inject customized HTML into your form, I am working on a CreateView Form which implements some arbitary creation of related elements, I inject the HTML under my field the following way:
HTML(
'''<p class="add">
{% trans 'Add a category' %}
</p>'''
),
My problem is that although url tags work ok, trans tags are not parsed within crispy forms, is there an alternative (maybe within crispy forms?) to maintain i18n?
In the form template there is proper i18n loading of tags:
{% load i18n %}
{% load crispy_forms_tags %}
Since you are trying to do it in some .py file (as i understood), then why do you bother with templatetags - use python.
injected_html = u"<p class='add'><a href='%(url)s'>%(translation)s</a></p>" % {'url':some_get_url_method(), 'translation':_(u"Add a category")}
HTML(injected_html)
What I want to do is include a form from a separate template at the bottom of a given page, lets say; "example.com/listdataandform/".
The form-template "form.html" displays the form as it should when the view is included in the URLConf. So I can view with "example.com/form/"
What I have so far goes something like this:
{% extends "base/base.html" %}
{% block title %} page title {% endblock %}
{% block content %}
<h2>some "scene" data</h2>
<ul>
{% for scene in scenes %}
<li>{{ scene.scene }} - {{ scene.date }}</li>
{% endfor %}
</ul>
{% include "tasks/form.html"%}
{% endblock %}
The code inside "block content" works as it should, since it is defined with it's corresponding view for the url "example.com/listdataandform/".
{% include "tasks/form.html"%}: This only displays the submit button from form.html, as expected. I realize by only doing this: {% include "tasks/form.html"%}, the corresponding view method is never executed to provide the "form"-template with data.
Is there any way to this without having to define the view to a specific pattern in urls.py, so that the form can be used without going to the that specified URL..?
So I guess the more general question is; how to include templates and provide them with data generated from a view?
Thanks.
For occasions like this, where I have something that needs to be included on every (or almost every) page, I use a custom context processor, which I then add to the TEMPLATE_CONTEXT_PROCESSORS in settings.py. You can add your form to the context by using this method.
Example:
common.py (this goes in the same folder as settings.py)
from myapp.forms import MyForm
def context(request):
c = {}
c['myform'] = MyForm()
return c
You can also do any processing required for the form here.
Then add it in your settings.py file:
settings.py
.
.
TEMPLATE_CONTEXT_PROCESSORS = (
'''
All the processors that are already there
'''
"myproject.common.context",
)
.
.
I realize by only doing this: {% include "tasks/form.html"%}, the corresponding view method is never executed to provide the "form"-template with data.
Indeed. You included a template, and it really means "included" - ie: "execute in the current context". The template knows nothing about your views, not even what a "view" is.
How does this help me executing the view for the included template to provide it with form data?
It doesn't. A Django "view" is not "a fraction of a template", it's really a request handler, iow a piece of code that takes an HTTP request and returns an HTTP response.
Your have to provide the form to the context one way or another. The possible places are:
in the view
in a context processor (if using a RequestContext)
in a middleware if using a TemplateResponse AND the TemplateResponse has not been rendered yet
in a custom template tag
In all cases this will just insert the form in your template's context - you'll still have to take care of the form processing when it's posted. There are different ways to address this problem but from what I guess of your use case (adding the same form and processing to a couple differents views of your own app), using a custom TemplateResponse subclass taking care of the form's initialisation and processing might just be the ticket.
Good evening people. I have this Django question.. I have a view that renders a form with a header information from a master-detail like models. Below this form, I want to load another view that renders a form that fills the detail table and next to it, the list with the detail. I want to load inside the template of the header view, the detail's view but I want to do it without ajax. I was reading the {% include %} tag but, from what I understand, it loads a template, not a view. I might be wrong, is there a way to load a view inside the master's template?
Thanks in advance
You couldn't render a view like that without ajax. {% load %} is used for loading modules, not templates or views, like:
{% load humanize %}
However, you could use an {% include %} tag, or a custom template tag that does whatever processing you need and returns its template where you include it.
I am trying to customize the output of the comments list and forms using Django's comments framework.
Inside my own template, should I try to customize the comments by doing something like {% get_comment_form for object as form %} and carefully construct the form based on the form variable or should I override the form.html template and simply call {% render_comment_form for object %}?
Currently I'm leaning more toward using {% get_comment_form for object as form %} inside my own template, but using form.html as a guideline to write my own form.
On my websites, overriding form.html (and other comments templates) and simply calling {% render_comment_form for object %} works very well.
You should definitively have a form.html that is customized for your website's design. And in case you want a comment form to have a particular design, you can use get_comment_form.