Using Nunjucks in flask app - Trying to include HTML - flask

I'm using Nunjucks apart from Jinja2 in my Flask application so I can pass in some variables through JS that I want to render in an HTML template.
-- Here's what I do --
JS controller:
this.element = DomHelper.htmlToDom( slideTemplate.render({ slide : this.model }));
{% include "presentation/slide/layouts/layout-1.html/" %}
What I have working:
Nunjucks compiles & render works properly without the {% include..}
slide variable is being passed and used fine
Any thoughts or suggestions would be nice. Thanks!

So I found that I had overlooked a simple thing. My nunjucks was configured to work only for the client side but the templates are served through flask. The relative path will only work for the client side data.
Solution: I placed the template layouts inside the static/ directory instead of in server-side templates/

Based on your question, you might have a typo. You have:
{% include "presentation/slide/layouts/layout-1.html" %}
but you say:
The desired html template I want to includes is in
templates/presentations/slide/layouts/ - Here's my folder structure
Is the path templates/presentations OR templates/presentation ?

Related

Django template nested include passing variables

I use django template index.html to render the frontpage. It includes another template to create a link icon. This template url_icon.html includes another template icon.html. When passing the arguments down the way, I face with an error. How to fix it?
index.html
.
.
.
{% include "url_icon.html" with name="return" url="/" %}
.
.
.
url_icon.html
{% include "icon.html" with icon={{ name }} %}
icon.html
<img src="/static/images/{{ name }}.png" />
Causing an error:
Could not parse the remainder: '{{' from '{{'
it looks like there are a few things you can do to improve/fix this. Addressing #1 and #2 should fix your issue. I've also added suggestions for best practices that would probably require refactoring (#3, #4).
It looks like you need to remove the curly-braces from name inside the {% include %} tag. Context variables can be used inside tags without extra syntax.
url_icon.html:
{% include "icon.html" with icon=name %}
icon.html will have access to name since you're not using the only keyword when updating its context, so your code might appear to work at first ({% include %} documentation). However, it looks like your intention is to refer to it as icon.
Use the variable icon in instead of name
icon.html:
<img src="/static/images/{{ icon }}.png" />
Optional suggestion: Use Django's staticfiles system
Try using the {% static %} tag for your icon. This will help make deployment easier, especially if you use a separate CDN from your webserver. There's lots of literature on how to set up staticfiles for Django projects in production, it's a large topic, but you'll be able to approach it more easily if you use the {% static %} tag from the beginning.
Optional suggestion: Django's URL routing system
Your route in index.html is hard-coded to be "/". Django has a powerful URL referencing system to leverage. If you've defined the root URL / using Django too, you can refer to it by name. Docs: {% url %}, and for the back-end, reverse().

Django Combine two templated from different apps

I have two apps with the following structure
project->
main app->
templates->
dashboard.html
my app->
templates->
mydashboard.html
I want to include mydashboard into dashboard, well this is possible using include template tag. but my problem appears when I have to pass some parameters into mydashboard, let's say they are named param1 and param2 these parameters are the variables which I have to load from my app app. well one possible method is to filling these params in dashboardview in mainapp and pass them using include tag into mydashboard.html like below
def user_dashboard(request):
...-->here I have to get data from my app (this view is in main app and I do not want to make main app be dependent to my app
return render(request, 'dashboard.html', {'param1': 0, 'param2': 34})
then in dashboard.html I add this part
{% is_app_installed "myapp" as is_myapp_installed %}
{% if is_myapp_installed %}
{% include "myappdashboard.html" with param1=param1 param2=param2 %}
{% endif %}
It seems the above method works but the main problem is that using this method mainapp is dependent to myapp and I do not want to this happens.
is there any other method to load those param1 and param2 inside myapp?
thanks
I can't see the details of the templates, but if you want to decouple the two templates I suggest you to rethink how you organise your templates logic. Try to rearrange the logic adding a base_dashboard.html in a common template dir with snippets and base templates to share between the apps.
Then the myappdashboard.html and dashboard.html can extend it with {% extend base_dashboard.html %}.

Angular-ui-router not rendering templates with django

I am working on a django project that relies on angularjs and having trouble implementing angular-ui-router framework.
As mentioned in documentation I have included ui.router as a dependency,
app = angular.module('myApp',['restangular','ui.router',])
configured the states as follows,
app.config(['$stateProvider',function($stateProvider){
$stateProvider.state('landing',{
url: '/',
template:"<p> somethings here.</p>"
})
}]);
in base.html file i bootstrap the django project with angularjs as required
ng-app=myApp.
and in index.html which inherits base.html
<div ui-view>
<i>nothing here but this text</i>
</div>
my urls.py,
url(r'^$',home,name="homepage")
This does not work, ui-router never includes the inline template in index.html. index.html always loads nothing here but this text. I have looked at as much questions asked here but are not helping. What am I missing, is this specific to django?
I would say that these lines should make it:
app.config(['$urlRouterProvider',function($urlRouterProvider){
$urlRouterProvider.otherwise('/');
}]);
Check the working plunker here
Also check:
otherwise() for invalid routes

gae_mini_profiler {% profiler_includes %} gives Invalid block tag: 'profiler_includes'

I am attempting to install gae_mini_profiler in my django-nonrel app
I placed the {% profiler_includes %} tag at the bottom of my base.html
It results in a
Exception Type: TemplateSyntaxError
Exception Value: Invalid block tag: 'profiler_includes'
I placed
from gae_mini_profiler import profiler
application = profiler.ProfilerWSGIMiddleware(application)
at the bottom of djangoppengine/main/__init__.py
I followed all the other instructions at https://github.com/kamens/gae_mini_profiler#start
What am I doing wrong?
I solved this by changing gae_mini_profiler/templatetags.py to be a true template tag library.
To do this create a package called templatetags, and then move(and rename) the templatetags.py module to profiler_tags.py.
Inside of profiler_tags.py make the following changes:
Change:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
To:
from django.template import Library
register = Library()
Change:
path = os.path.join(os.path.dirname(__file__), "templates/includes.html")
To:
path = os.path.join(os.path.dirname(__file__), "../templates/includes.html")
In your settings file add gae_mini_profiler to your list of installed apps.
Remove all references to
template.register_template_library('gae_mini_profiler.templatetags')
In your templates whereever you had {% profiler_includes %} you then need to add a load block
{% load profiler_tags %}
I think that is all of the changes, but need to go check my git log.
Are you using the new Python 2.7 runtime for GAE? If so, the django template setup is slightly different and gae_mini_profiler hasn't been upgraded yet (anyone is welcome to submit this fix, haven't gotten to it yet).
It should be easy to work around as the only thing you need to do is find a way to render the HTML string returned by gae_mini_profiler.templatetags.profiler_includes() anywhere in your page. There are a number of methods of accomplishing this if the built-in template tag isn't working as-is. You could simply call the function in your base request handler and pass the resulting html into your base template if absolutely necessary (although this is admittedly a gross hack).
We'll hopefully have Python 2.7 working w/ gae_mini_profiler shortly. If you're not on Python 2.7, I'm not sure what the issue is as I'd expect the current code to work...

Difficulty overriding Django Admin template

I'm using Django 1.2.4 on Ubuntu 10.10. I'm trying to override the index.html template for the admin module. I've been following these instructions. I also looked at this question, but I'm still having difficulty.
The instructions say to create an admin directory in the templates directory:
templates/
admin/
index.html
I want to override a single block in the index.html. (Really, all I want to do is append some text to the end. Is there an easier way than copy/pasting the entire block and changing it?) (Update: Looks like {{block.super}} may help.)
To signal that I'm overriding, I put this at the top of my index.html:
{% extends "admin/index.html" %}
Of course, that results in a stack overflow (from the terminal):
Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type 'exceptions.RuntimeError'> ignored
What is the correct way to do this? I tried a symlink per an answer on the linked question, but that resulted in the following:
me#mycomp:~/foo$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/ django_admin
[sudo] password for me:
ln: creating symbolic link `django_admin': Protocol error
What am I doing wrong?
The recursion error is because you're extending the admin/index.html with itself.
You can either:
copy the entire admin/index.html template in your templates/admin/ directory, and it will replace the default template with yours
override the index.html per app or model, as explained here
I know this is late after the question, but you know, google traveling...
Amend settings.py with an extra template folder, for example:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/mysite/webapps/django/myproject/templates",
"/home/mysite/webapps/django/lib/python2.7/django/", # extra folder
)
Then in myproject/templates/admin add your own index.html like:
{% extends "contrib/admin/templates/admin/index.html" %}
{% block branding %}
<h1 id="site-name">Administration for TheLittleButtonCo</h1>
{% endblock %}
Variations are possible, obviously. This works on Django 1.3.1 final
Not sure if you found the answer, but you need to change
{% extends "admin/index.html" %}
to
{% extends "admin/base_site.html" %}
as that is what the original index.html page overwrites. Because the Django system searches your templates folder before using the default admin one, so in this case it finds the admin/index.html in your templates, then it's trying to extend itself with the extend (hence the recursion error).
For reference you can customise the base_site.html in you templates too, it extends base.html. The best thing to do is copy the original from:
/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/
and paste it into your templates folder as a starting point
I use an extra package, called django-smart-extends