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

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"

Related

Best way to store web page content in database using Django and a single template for web pages

I'm building a web site and the bulk of the content will be the same general type and layout on the page. I'm going to use a single template to handle each post and the actual content will be stored in a database.
The content will just be html paragraphs, headers, sub headers, different lists, quotes, code blocks, etc.
Web pages will typically be the same or at least similar. All html components should follow the same guidelines to make sure everything looks and feels the same. Currently I'll be the only author, but in the future I plan to incorporate other authors as well.
At first I thought, just copy and paste this html content into a textfield in the database and I can add new posts/articles on the admin site.
Then I thought, maybe use a textfield and copy and paste json of a list of ['type': , 'content': ]. and then I can have the single template page iterate over this list and display the content based on the 'type'. My idea here is that it would shorten the data I have to add to the database by stripping the html tags out of the equation.
Considering I hope to have future authors as well, just curious of some ideas on how I can accomplish this to make it easy for myself to post new content.
That sounds pretty much exactly like the example of this fantastic tutorial by Miguel Grinberg. He sets up a flask environment to be used as his personal blog. With user log in and everything you would need.

Django - Avoid writing duplicate code

I want to create 2 web pages which use the same code but I don't want to copy/paste the code in 2 places. I'm sure I don't need to have the same code in 2 different places to use it on multiple pages. Can someone suggest a way to avoid creating duplicate code?
If required, I can provide some sample code which I've already written.
For generating HTML on the frontend, you can use templates to create static content which can be used across multiple pages on your site.
For more information on how to use django templates, see: https://docs.djangoproject.com/en/1.9/topics/templates/
As for the backend, you'll need to write your code as re-usable as possible and then import/reuse as much as you can. Pylint has a check for similar/duplicate code:
For more information on Pylint similarities-checker, see: https://docs.pylint.org/features.html#similarities-checker
Use this built-in django template tag for that:
{% include "subtemplate.html" %}

Can Pelican support multiple article indices in different templates?

I'm trying to investigate if I can use Pelican to generate my site and having trouble seeing how to present two different lists of articles, each rendered in their own template.
My site has blog pieces, feature articles, and white papers. The index page for each of these needs to be rendered in a different style.
After a little playing around, it appears to me that I would probably need to have a category for each article-type. Is it possible to tell the engine to render each category list using a different template.
Alternatively, and perhaps better for increased flexibility, is it possible to create a page that loops through a category and i control the display of the article list? I could then solve the problem by creating a page of my own for each category. Does a page have access to the pelican context (i.e. the catalog of content built by the generators' generate_context methods)

Issues with multiple languages in mezzanine

I am using multiple language in mezzanine and it is working fine with its own core templates.But I try to use it in my own templates then it is not working.
I have use all things in proper steps. I have locale folder in which django.po consists of all text which I can translate and there encoded django.mo file.
Buy when I choose other then English Lang. in my browser it does not translate my templates text but change the text of core templates.
I don't know why it does not translate my templates data Because if there are any kind of errors then it must not translate core templates but it does.
Please help me to sort out my problem , any suggestion would be appreciated.
Thanks.
Does your .po file contain the word "fuzzy"? If so, you might want to read the following article: http://blog.e-shell.org/124, as mentioned in this previous SO question: Django missing translation of some strings. Any idea why?
If that is not the cause of your issue, are you sure you are using the internationalisation template tags appropriately?
First of all, ensure you are loading the relevant template tags at the top of each of your templates:
{% load i18n %}
Then, for any text you want to make translatable:
{% trans "Insert your text here" %}
See also: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#internationalization-in-template-code and https://docs.djangoproject.com/en/dev/topics/i18n/translation/#implementation-notes.

Including Flash content inline in a custom Weblog?

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.

Categories

Resources