Loading an external template in GAE application? - python

Is there a way to load a template from a source which is not hosted on the application itself? For example, in order to load a template on the application itself (hosted by the appengine app):
html = template.render("admin/my_template.html", params)
I would like to do something like:
html = template.render("http://www.otherhost.com/external_template.html", params)
Is there a mechanism to allow such a behavior?
Thanks

Ofcourse you can load templates from other apps. To load templates you have to fetch the templates.
But using an editor is also not very difficult. I use codemirror to edit a HTML textarea.
See this list of demos : http://codemirror.net/demo/
Editing a Jinja txt mail template example:

Templates are just strings. If you can get the text, you can parse it as a template.
In this case you would need to make a request for the file using urllib, get the response, and then use template.Template(content) to convert it to a template object.
However, I must say I still think keeping it in the datastore is a better bet. I'm not sure why you would need an editor for that - why not just cut and paste the content into a field in the GAE admin console, or using the remote API to set it?

Related

How to hide an external url from my user in a Django project?

I have a model in my django project that uses the UrlField to keep external urls files. I use the #login_required in my view to prevent anonymous users to get access to this content. But, in this case, a logged-in user can retrieve this information and post anywhere for everyone to use.
My intention was to create a specific path on my urls.py, with the model id as one of the parameters, and in the view called by this url it would fetch the content in the UrlField and serve the user without display any external link. Something like a proxy or mask for external urls.
Does anyone know how to do this?
I've tried to use HttpResponseRedirect, but in the end the browser keep showing the url I want to hide.
you have to make your statics files server talk with django, maybe with this
https://github.com/johnsensible/django-sendfile

TemplateNotFound: theme_base.html

I'm trying to set up a webpage, and one of my html files begins with {% extends "theme_base.html" %}. When I try to run the program, I get an error message saying "TemplateDoesNotExist" for theme_base.html. I don't think this is a file that I have to create. Is there something I need to install to access this html file? I've already got pinax_theme_bootstrap installed. I thought that that was what I needed, but the error is still there.
Usually with Django, websites have a base theme, which other pages use to display content so you don't have to recode things like the header and footer. This is the basis of most front-end frameworks.
The question is, why does your HTML file extend a base template that doesn't exist? It sounds like you're trying to alter pieces of a project without the underlying files to the project.
The django docs are really great, and if you're new to django going through their quick tutorials will help you substantially:
https://www.djangoproject.com/start/

Django return json and html depending on client python

I have a django app which has html templates and I also have a command line python api which can do GET and POST requests to the django app on the server. The api can pretty much do everything that the django app can do. How do I make it such that when I access the django app through the browser it returns html but when I access it through the api it returns json?
Where will I have to put the json and what changes do I have to make to my app?
Thank you
Use different URLs for the JSON and HTML versions.
I suggest that your JSON version be available on a url like r'normal/api(?P<json_flag>/json/?)$', and have a parameter in your view to receive the json flag. You can then serve appropriately.
Naturally, your view will have to use different logic to generate HTML and JSON. I strongly suggest that you use the json module instead of a template to generate JSON.

Why should JavaScript files be localised differently in Django?

When localising Django application the makemessages command simply parses all the TXT, HTML and PY files and generates PO files for them but when localising JS files, you need to run the djangojs command. I haven't delved into the Django source to figure out why this done differently. Could someone explain?
I've read that in production environments, Apache is used to serve the application files while a simple proxy like Nginx is used to serve static files as this greatly reduces the load on the application server. With this scenario, I guess it works like this: when rendering a template, Django checks the requested locale, loads the appropriate localisation file and serves the template but JS on the other hand being served as static media doesn't get parsed by Django. Is this it?
(Its my first foray in to the world of localisation with Django and I'm packed full of question, many of who's answers I can't seem to find and therefore this post.)
Thanks
The reason why it's handled differently is in the docs.
Adding translations to JavaScript poses some problems:
JavaScript code doesn't have access to a gettext implementation.
JavaScript code doesn't have access to .po or .mo files; they need to be delivered by the server.
The translation catalogs for JavaScript should be kept as small as possible.
So essentially, the internal Python translation is done on the server. But for JS, there's another file served by the server, which contains all the required translations for user's language. And the translation is done on the user's side. So as you can see, it's a completely different strategy. Django helps by adding similar interface for JS files, even though they're handled in a completely different way.
I guess it works like this: when rendering a template, Django checks
the requested locale, loads the appropriate localisation file and
serves the template but JS on the other hand being served as static
media doesn't get parsed by Django. Is this it?
You are right in the first part, about handling templates. Handling JS works as I've explained above.
Note that Django JS translation mechanism, doesn't treat JS translations as static files. It uses a Django view to generate the JS file everytime (javascript_catalog mentioned in the docs linked in the first line).
That is one of the problems I've encountered. Such files don't need to be generated on every request. There are some projects that actually let you pack those JS translations as static files and enable you to cache them properly (like django-mediagenerator).

Django templating engine and external js files

I'm writing a Google app engine app and obviously the default web app framework is a subset of Django. As such I'm using it's templating engine.
My question is if I have say the following code:
template_values = {
'first':first,
'second':second,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
And I want to reference the template value first in an externally included javascript file from index.html, do I just continue with the {{ first }} usage I'd go with in index.html itself or do I need to tell the framework about example.js somehow too so that it knows to replace the reference there?
Inserting the value into the javascript is probably a bad idea; wouldn't it make more sense for the script to be static and to have it grab the data either out of the DOM (assuming it's part of the HTML page you're rendering) or get the necessary data from the server using an AJAX call?
Disclaimer: my knowledge of app engine is limited so this answer may not serve your purpose.
In Django if you need to be able to pass variables from a view to a JS file you will have to ensure that the file is parsed by the template engine. In other words the file has to be served by Django. This means for e.g. that if you have a JS file in a media directory that is served by say, Nginx, you will not be able to use template variables in it.
I would expect the same to apply for app engine. But then see the disclaimer. I might be totally wrong :P

Categories