using angular expressions in a django based webapp - python

I recently started with django and I have experience with angularJS. I got pretty confused by the static media rendering of Django which uses the same moustache ({{ }}) as angular. So how to use angular expressions while working with django..
<body>
<h1>Including Static Media</h1>
<img src="{% static "rango.jpg" %}" alt="Picture of Rango" /> <!-- Images -->
</body>
Instead of using the rendering as it is, can i use something like
image src="/static/images/xyz.jpg"
However if I want to load my images or js script dynamically or if I have to make URL's dynamically using something like ng-href="#!/{{redirectService(parameters)}}". It seems that syntax of both frameworks conflict alot. Can anyone help me regarding this.?

Check out the {% verbatim %} tag and/or the {% templatetag %} tag.
From the docs:
verbatim
Stops the template engine from rendering the contents of this block
tag.
A common use is to allow a JavaScript template layer that collides
with Django’s syntax. For example:
{% verbatim %}
{{if dying}}Still alive.{{/if}}
{% endverbatim %}
You can also designate a specific closing tag, allowing the use of {% endverbatim %} as part of the unrendered contents:
{% verbatim myblock %}
Avoid template rendering via the {% verbatim %}{% end verbatim %} block.
{% endverbatim myblock %}
templatetag
Outputs one of the syntax characters used to compose template tags.
Since the template system has no concept of “escaping”, to display one
of the bits used in template tags, you must use the {% templatetag %}
tag.
The argument tells which template bit to output:
Argument | Outputs
————————————————|————————
openblock | {%
closeblock | %}
openvariable | {{
closevariable | }}
openbrace | {
closebrace | }
opencomment | {#
closecomment | #}
Sample usage:
{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}

Related

Need help identifying language, I believe its Python, but I'm not sure

I just took a part time job as a web developer and need help identifying some code. They told me it was built in html/css/javascript, but they are using what I believe are python template tags. They are comfortable with me learning a new language, I just want to make sure that I'm learning the correct language.
I've copy and pasted the code into google and stack overflow search bars, again it looks like python/django or possibly jinja 2, but I don't know those languages and want to make sure I'm on the right track. This is just the opening line of the master.master file, which I am also not used to seeing as a master file.
`
<!DOCTYPE html>
<html lang="{{ data.Language }}" dir="{% if data.LanguageDetails.IsRtl
%}rtl{% else %}ltr{% endif %}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
{% block meta %}{% endblock %}
{% for language in data.Languages %}{% if language.Culture !=
data.Language %}
{% assign currentLanguagePrefix = '/' | Append:data.Language | Append:
'/' %}
{% assign thisLanguagePrefix = language.Culture | Append: '/' %}
<link href="/{{ data.URL | Replace: currentLanguagePrefix,
thisLanguagePrefix }}" hreflang="{{ language.Culture }}" rel="alternate">
{% endif %}
{% endfor %}`
This is Liquid. The syntax is very similar to django template language, because liquid (and jinja, twig, nunjucks) was directly inspired by django.
Liquid has some features that doesn't exist in django template language, for example the assign tag and append filter. Here's an example from the Liquid docs:
{% assign filename = "/index.html" %}
{{ "website.com" | append: filename }}
Liquid is written in Ruby used in many projects, and is ported to other languages (but not to python).
Probably not Jinja2, but instead Django.
In Django method calls work implicitly, while Jinja requires the explicit Python syntax. Thus this Django code:
{% for page in user.get_created_pages %}
...
{% endfor %}
...looks like this in Jinja:
{% for page in user.get_created_pages() %}
...
{% endfor %}
More examples.

integrate html into django template

In my django model I have to store html data as string like
item = RocketuItem(text=text, page_url=url, page_number=page_number, page_down=page_down)
and text =
<section> <h2>Object-oriented Python and Beginner Django</h2> <ul>
When i pull this in template i use ..
{% for page in pages %}
<div>{{ page.page_url|safe }}</div>>
<div>
{{ page.text|safe }}
I m using safe as it dosent display $lt etc [basically '<' gets converted to &lt if i dont use it]
This is wht i see at output on the page
<section> <h2>Object-oriented Python and Beginner Django</h2> <ul>
What i really want is for the code fro 'text' to be a part of the div so that it well get that formatting and show..
Object-oriented Python and Beginner Django
How do i do that ..
The safe filter should be enough to render the text as html in recent versions of Django. Try using:
{% autoescape off %} {{ content }} {% end autoescape %}
tags and see if that works for you.
Use this:
{% autoescape off %}{{ page.text }}{% endautoescape %}
Autoescape documentation

Django unable to understand template html format

I am learning Django framework, I setup Django on my local host and successfully run my first app, and i downloaded some web app from git and then i tried to understand it but when i opened template html the format used their was completly different from original html format, so i just want to know what scripting, or language they are using to build html files, Here is a sample code
{% extends "admin/change_list.html" %}
{% load i18n %}
{% block object-tools-items %}
{% if not is_popup %}
<a href="{{ recoverlist_url }}" class="recoverlink btn">
<i class="icon-repeat"></i>
{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{ name }}{% endblocktrans %}</a>
{% endif %}
{{block.super}}
{% endblock %}
I am unable to get what {% %} doing here, Is it a scripting tag just like jsp or something else, If it is a scripting that what kind of script is it ? because normally html files starts with
<head>
tag.
Is it a scripting tag just like jsp or something else
Yes.
If it is a scripting that what kind of script is it ?
Django Template language
because normally html files starts with <head> tag.
It's not HTML. It's a template language that render and outputs HTML.
I'd suggest you go through the Django tutorial.

Is it possible to have Django custom template tags load other templates in package

What I'm looking to do is package templates in a Django package which can be inserted on a developers page by simply using
{% load app_tags %}
this works find for custom methods which take a value and return a value. What I would like to do is simply have a method which returns a template packaged with the app.
{{ custom_template }}
So the question boils down to how do I have a project which installs my app load my apps' tags and call a tag method which includes a template from the app.
thank you for any responses.
yup! make an inclusion tag in app_tags.py, and then call it!
they're great for code reuse (along with Django blocks and the {% include ... %} template tag, of course)
reference: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
# app_tags.py
from django import template
register = template.Library()
#register.inclusion_tag("templates/myappname/greeting.html")
def greet(name, end="!!!"):
return { 'name': name, 'end': end }
and
{# templates/myappname/greeting.html #}
<h1> What's up {{ name }}{{ end }} </h1>
then to call this, you'd use {% and %}, the double bracket notation e.g. {{ custom_template }} is really only for showing the value of a single variable
{% load app_tags %}
{% for person in people_to_greet %}
{% greet person %}
{% end %}
<h3> cool greetings above ^ </h3>

Is it possible to load a custom template tag in base and use it in extented templates

I loaded a custom template tag note_extras.py in base.html.
base.html
<div id="wrap">
{% load note_extras %}
{% block content %}
{% endblock %}
</div><!--wrap-->
but it is not accessible at templates which is an extend of base.html ie::
home.html
{% extends "base.html" %}
{% block content %}
<div class="container">
{% create_tagmenu request.user.pk %}
</div>
{% endblock %}
it is working fine if i load note_extras in home.html ie:
{% extends "base.html" %}
{% load note_extras %}
....
In Django template language, you must load all needed template libraries in each of the templates.
I personally think it is a good idea because it makes templates more explicit (which is better than implicit). Ill give an example. Prior to Django 1.5, the default behavior for a url tag was to provide the view name in plaintext as well as all the needed parameters:
{% url path.to.view ... %}
There however was no way to provide the path to the view via a context variable:
{% with var='path.to.view' %}
{% url var ... %}
{% endwith %}
To solve that, starting with 1.3, you could import the future version of the url tag (which became the default in 1.5) by doing:
{% load url from future %}
{% url var ... %}
or
{% url 'path.to.view' ... %}
Now imagine that you would need to create a template which would extend from a base template which you did not create (e.g. one of django admin base templates). Then imagine that within the base template it would have {% load url from future %}. As a result, {% url path.to.view ... %} within your template would become invalid without any explicit explanation.
Of course this example does not matter anymore (starting with 1.5) however hopefully it illustrates a point that being explicit in templates is better than implicit which is why the currently implementation is the way it is.
If you want that a template tag is loaded in every template you want to do it in the init file of your app:
from django.template.loader import add_to_builtins
add_to_builtins('my_app.templatetags.note_extras')
In case anyone was wondering, add_to_builtins has been deprecated but one could still load a tag for all of the templates in the project via settings.TEMPLATES - supported for Django 1.9 onwards as described here:
https://stackoverflow.com/a/59719364/2447803

Categories