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!
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
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"
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am trying to self-learn developing Web applications using Python as backend. Since I am from C++ background I find difficulty in building web pages (design / implementation) and backend code associated to it – creation of CSS, HTML code, including Images, tables etc. I read about http://webpy.org/ framework but have not yet used it. In actual I am a bit confused how to develop a great UI page using Python – something like page having multiple tabs, color scheme, drop box, list, graphs and other UI component / widgets - and ofcouse backend code associated to it.
Can anyone please let me know what path we should take so that the same can be made easy? I read about JQuery and believe that in computing world there are a lot of tools like that available but which combinations stands the best and .. easy to work with.
You can't run python in the browser. So, for web development it's used exclusively on the server side. And really it's whole purpose is to enforce your business logic and generate the markup for your site. Then the client side technologies (HTML5+Browser, Javascript and CSS) take over.
On the server-side, Django is really popular right now. It is quite robust and has a very active community behind it. I would recommend that you look at the Django tutorial. For the client side, jQuery is very popular and has a HUGE community behind it. There are many, many tutorials out there - just google "jQuery tutorial".
If you are not very good with CSS (and it sounds like you might not be), then I would personally recommend one of the grid-based CSS frameworks. They make it a lot easier to get a professional looking site. And with the responsive frameworks, then you have the added benefit of being quasi-mobile enabled. There are a bunch of them. Including one of the originals Grid 960, but again there are many. Here's a pretty good blog post on 16 of them.
As for controls/widgets, there are several to choose from. jQueryUI is very good and popular. While not as popular, Dojo is still a good option to check out. ExtJS is good, but not free. And the list goes on... YUI, etc. You will probably just want to pick one with a good community behind it and learn it.
I think this contributes to the discussion as it helped me a bit in making Python great for the web.
Metalfan, I saw you asking on the chat:
Are there any libraries which makes Python work like PHP. I mean embed
into HTML something like
You're asking about a templating library to build html, css, javascript, sql or anything else. I've searched for a tool to do that, and found Cog, Cheetah and most of what else Google throws at you. I was inspired by what I was in Django, which gives the template processing access to objects and basically the whole language, instead of only doing macro-expansion.
Web2py has a module that does just that, and I've found it very easy to modify or use stand-alone. I can't remember if I've made any modifications to the original, so I've uploaded the version I'm using, so it should work out of the box.
To generate code from a template, you include the template module, and call the render function with a template (string) and a context (dict containing local environment). Like this:
from template import render
import urllib2
environment = dict(
elements=[1, 2, 3],
username="mortn",
session_id="xyz",
lam=lambda x=0: urllib2.urlopen("http://google.com").read(),
f=f,
)
print render(content=html_template, context=environment)
The template would then look like this:
<html>
<h1>Hello {{= username }}</h1><br/>
{{ if session_id=="xyz": }}
{{ # indentation doesn't matter.. }}
this isn't printed unless if-statement matches
{{ else: }}
instead this would be shown
{{ pass # each if/for/while statements (that would
{{ # "indent your code"), must end with pass }}
{{ for e in elements: }} - {{=e}}
{{ pass }}
{{ # demo of looping }}
{{ if 1: }}
{{ for i in xrange(10): }}<br/>{{ pass }}
{{ pass }}
{{ # imported function }}
100 chars of google html get:
{{ =lam()[:100] }}
{{ # you can access the whole language with this }}
{{ =f()}}
.... and it gives THIS output:
<html>
<h1>Hello mortn</h1><br/>
this isn't printed unless if-statement matches
- 1
- 2
- 3
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
100 chars of google html get:
<!doctype html><html itemscope="itemscope"itemtype="http://schema.org/WebPage"><head><meta itemprop
you can do whatever
It messes a bit with the white spacing as you'll see, I've shortened the output down as each {{ block generates a newline. It can be edited in the template.py module as it has VERY readable code that adheres to PEP8.
TL;DR:
Here's the link for the sample code in a single file:
http://pastie.org/4776120
and the template.py module: http://pastie.org/4775988
First, check [w3schools.com][1] out,
Read on about 1)HTML, 2) CSS, 3) JavaScript 4) PHP
If you really want to build web apps fast, i'd recommend you to choose php over python, because php especially designed to build dynamic web apps.
I'm trying to think of a way to place Flash content into a blog post so that it appears inline between paragraphs. I'm writing a custom weblog application in Django (still learning) and I'll be using SWFObject for the embedding.
The blog is for me only so the back-end isn't too fancy. I'm simply using Django's built in admin interface. No TinyMCE rich text editor (like Wordpress), rather I've implemented Markdown.
I'd like to add Flash content into the body of a post, between paragraphs, in a way that is not coupled to any third party script. Meaning, I would prefer not to include javascript within the body of the blog post as it introduces a dependency on SWFObject. For example, I could quite easily add the following to an entry via the back-end to embed a SWF inline:
Paragraph one...
<script type="text/javascript">
swfobject.embedSWF("/path/to/flash.swf", "myContent", "200", "200", "9.0.0");
</script>
<div id="myContent"></div>
Paragraph two...
As you can see this is quite wordy and a lot to remember but it also refers to SWFObject directly. This WILL work, however I would prefer to write it in a "cleaner" more abstract way. What I was thinking of doing is creating my own parser which would translate a custom string into the above just before rendering a template.
[#SWF swf="/path/to/flash.swf" w="200" h="200" ver="9.0.0"]
I'm wondering if anyone has encountered this issue. I'd love to know how you solved it.
You might want to look into OEmbed, specifically the django-oembed project.