Django - Avoid writing duplicate code - python

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" %}

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!

Use jinja for generating python class

I have a project that needs to automatically generate a python class according to some configuration files using python.
During my searches, I became familiar with Jinja2 which seems to be very popular for generating web pages, but I couldn't really find a similar case which uses Jinja to generate some python codes using Jinja (I know that it is definitely possible to do it, just the lack of examples made me hesitated).
Is it make sense to use Jinja2 for my case or is there any easier solution for generating python from python?!
You can use jinja to generate any text. I use jinja myself to generate python and there is at least one previous stack overflow post.

update text within html

So I have a python webapp on Google App Engine and am using the jinja2 template engine. I have a lot of text on the site that I want to update regularly, such as news sections and updates about the site.
What is the most efficient way to go about doing this? Clearly the simplest short-term solution and what I am currently doing is just to change the HTML but I would like to give others access to this without giving them access to the server side of things.
Should I just bite the bullet and write a interface on an admin page that allows users to edit it and then the server takes this and renders it in the News section? Any suggestions or tips would be great!
What you are thinking about, and moving toward (whether you know it or not) is called a content management system.
Most of them store content in a database and provide a user interface to allow editing it, just as you're designing.
Perhaps you could use off-the-shelf parts? I don't know exactly which ones are appengine-based, but this is a very common task and I'm sure you'll save time by using others' work.
I have created a very basic jinja CMS to maintain Jinja (page) blocks. You can find it here: https://codereview.stackexchange.com/questions/5965/review-request-jinja-cms-for-energiekantoor-nl-on-google-app-engine

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

Categories

Resources