Can I nest multiple template objects inside another chameleon template? - python

Say I had a chameleon template file for a user object with something like this:
<h2><tal:content="user.name"></h2>
<h4><tal:content="user.occupation"></h4>
<p><tal:content="user.bio"></p>
Can I loop over a list of users in another template file to fill multiple content slots like below?
<tal:block repeat="user users">
<div metal:define-slot='user'></div> <!-- ??? -->
</tal:block>
I imagine this may be useful if a page has a list of complicated objects with a lot of data to display, but I can't find anything about it and I don't know what search terms to give google.
Right now I just have something like this:
<tal:block repeat="user users">
<h2><tal:content="user.name"</h2>
<h4><tal:content="user.occupation"></h4>
<p><tal:content="user.bio"></p>
</tal:block>
which is good enough for me, but I was just wondering if what I am asking is possible.

What you're looking for are metal macros. They are confusing at first, but super powerful once you get your head around them.
https://chameleon.readthedocs.org/en/latest/reference.html?highlight=metal#metal
HTH

Related

How to dinamically inject HTML code in Django

In a project of mine I need to create an online encyclopedia. In order to do so, I need to create a page for each entry file, which are all written in Markdown, so I have to covert it to HTML before sending them to the website. I didn't want to use external libraries for this so I wrote my own python code that receives a Markdown file and returns a list with all the lines already formatted in HTML. The problem now is that I don't know how to inject this code to the template I have in Django, when I pass the list to it they are just printed like normal text. I know I could make my function write to an .html file but I don't think it's a great solution thinking about scalability.
Is there a way to dynamically inject HTML in Django? Is there a "better" approach to my problem?
You could use the safe filter in your template! So it would look like that.
Assuming you have your html in a string variable called my_html then in your template just write
{{ my_html | safe }}
And don’t forget to import it!

How to make a html table using python?

Just received a task from a basic programming course in uni.
I am a complete newbie regarding computers. I am a freshman and have no prior programming experience.
The task requires making a python source code that would print a html table as output.
No use of modules is allowed.
We covered some basic python things like if, for loop, while, print, etc...
but didn't learn anything about creating html in python.
I've been searching on the internet for hours and hours, but all solutions seem so advanced and they all involve use of third-party modules, which in my case is not allowed.
Professor knows that we are all complete newbies, so there's got to be a way to do this without much professional knowledge.
Can anyone please tell me the basics of making a html table in python?
Like do I just type in things like
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
in python? Basically have no idea where to start.
** The code should be written in a way that when it is executed in bash shell ($ python file_name.py), it prints out a html table.
P.S. I'm using vs code as an editor for python.
As I can imagine the only way to make it simple without going bananas is to write
html file. So basically its .write rows into html file using python. If this is what you want - here is an example how to do that plain and simple:
from random import randint
with open('table.html', 'w') as html:
html.write('<table style="border:2px solid black;">\n')
for i in range(5):
html.write('<tr>\n')
for i in range(3):
html.write(f'<td>{randint(10,150)}</td>\n')
html.write('</tr>\n')
html.write('</table>')
"\n" - used to make you Html document look readable.
Random.randint module is used to generate data - its basic simple module just for data placeholders.
For the table to look nice you can add border to td -
<td style="border:2px solid black;" >(random data)</td>
It will look like solid html/excel table.
You won't learn anything by copy-pasting working examples that you don't understand and you can't expect to understand anything, when you search for solutions to complex problems without knowing the basics.
Given the level of experience you have according to your question, you should instead search for a Python tutorial to get a grip of the language. Read about the synthax, Python's object model and the type hierarchy. With that knowledge you will be able to understand the documentation, and with that, you should be able to solve your problem without searching for pre-made solutions.
As we haven't covered any HTML-related topic in class yet, doing the task doesn't require much complex solution. It looks like the task can be done just by using for loop, lists, if, print... etc, all of which I've already learned and am quite confident with. Many thanks to everyone who cared to give help and answers.

Google App Engine(Python) - site fragments - composite view

Is there a way to build three or four parts of a site (three or four html templates) and then render some of them or all of them together in GAE python? I know I can load and render one specific html django template but I want to build templates for different parts of the site in different files and then compose them together depending on the situation.
A good example would be that I want pretty much the same menu, header, footer in most of my web application pages but I want to switch a specific part of the content.
So I would like to have one file and template that deals with lets say classes and another that deals with students, so the general look of the site (main.html) stays the same but the way I display and handle the information about students or classes is completely different. I basically want to plant a bunch of page specific html into a generic template.
Thanks for any help on this. :)
I am not sure what is the correct technical term for what I'm looking for(I tried searching). => I think they call it composite view or site fragments in the Zend framework.
You should use template inheritance in Django. Have a look at this tutorial for a start.
EDIT The official Django Book section on Template Inheritance also demonstrates how different 'fragments' e.g. a footer, or a nav bar, may be stored in different template files and brought together via inclusion and inheritance.
This site shows how one template can inherit from another, as when a site section template extends a basic layout template, with the code, for example,
{% extends "base.html" %}
It also shows how using template inclusion one may, for example, add different pieces to a larger template like pieces in a puzzle. For example, a navigation fragment may be added to a layout file with the phrase
{% include "nav.html" %}
As noted in the comments by #Nick Johnson: extends is more compact and can make the use of multiple file fragments unnecessary. Only include as last resort, if extends fails you.
EDIT See also my answer to a question on "How to cut large HTML file into multiple HTML files"

Find untranslated strings in HTML templates

Is there way to find untranslated strings in the HTML templates of my Django application i.e. blocks of text that are not wrapped in trans and blocktrans tags.
Since we have many templates, it would be a very time-consuming process to go through them manually and check but if there isn't an option, I guess it has to be done the long and tedious way.
Thanks
Found this recently, but have not tried it yet.
Doc: http://www.technomancy.org/python/django-template-i18n-lint/
Code: https://github.com/rory/django-template-i18n-lint
Looks like it hasn't been updated in a year but it might provide a good starting spot.
You can use the builtin template parser to parse your templates, and recurse into all tags that are not instances of BlockTransTag

feincms applicationcontent not working with feincms_translatedpage_or_base

I have used the code from the examples in the docs, but I cannot make ApplicationContent show anything on a translated page.
The ApplicationContent is on both the base-language-page and the translated page, but it only shows up on the base-language-page.
The regions are being rendered like this:
{% feincms_translatedpage_or_base for feincms_page as feincms_transpage language=LANGUAGE_CODE %}
{% feincms_render_region feincms_transpage "main" request %}
Does anyone have any idea as to why this is?
Yes, merely rendering the content is not sufficient for ApplicationContent to do and/or show anything.
The list of content blocks has to be determined earlier so that process() and finalize() can be called on all content types offering these methods (f.e. ApplicationContent.process).
If you want to inherit content from the main translation you'd have to write your own ContentProxy subclass with a customized _inherit_from method. Please note that this method is undocumented, it probably wont go away without a very good reason though.

Categories

Resources