This is regarding Django tutorial - Part 2
http://docs.djangoproject.com/en/dev/intro/tutorial02/
In the section to change the template for admin page, I tried to change this section in the base_site.html page.
{% trans 'Django's administration' %}
When I add the apostrophe and s, I ge the error that -
TemplateSyntaxError at /admin/
only option for 'trans' is 'noop'
Why is it so? I thought I should be able to change the site's name. I tried using double quotes and escape sequence also, but it did not work.
I tried using double quotes and escape
sequence also, but it did not work.
That is definitely the problem. That is the only problem that the error message specifies!
Are you positive there are no other places where you've done that?
It should be:
{% trans "Django's administration" %}
That error messages only exists for the tag "trans" and appears if there is any other argument in the tag that is not noop.
The problem is the second single-quote:
{% trans 'Django's administration' %}
Django is treating everything after it as an argument. As the only argument it accepts is noop, this is causing an error.
One way to get around it is to do as Yuji 'Tomita' Tomita suggested, and enclose your translation string in double-quotes.
Another way is to use the blocktrans tag:
{% blocktrans %}
Django's administration
{% endblocktrans %}
As you're not using quotes to denote text which needs to be translated, this won't run into the same issue as the trans tag.
Related
Problem:
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block pagetitle %}
becomes
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block
pagetitle %}
Note that the {% tag %} is being broken with a new line. This causes syntax errors with django templates.
I've tried most top django template extensions and this does not fix the issue.
I've also tried these settings:
"[html]": {
"editor.formatOnSave": false,
},
"html.format.wrapLineLength": 0,
"html.format.enable": false,
"prettier.disableLanguages": ["html"]
Desired Behavior:
Automatically format *.html files, while preserving django template tags, not breaking them up with newlines.
Sub-optimal (but acceptable) behavior: don't format *.html files at all.
I had the same issue and the only way I found that solved it is to disable the default HTML formatter. Unfortunately, I did not find a way to make it format Django template tags correctly. You can do the same if you go to VS Code Preferences > Settings > User > Extensions > HTML and uncheck 'Enable/disable default HTML formatter'.
I solved this by following this advice: https://stackoverflow.com/a/73892745/1257347
TLDR: install the djLint extension (and remember to do $ pip install djlint)
I got it to work by simply adding {{""}} between the {% tag %} that were being broken.
Example:
{% extends 'main/base.html' %} {% block title_block %}Homepage{% endblock%}
{{""}} {%block style_ref_block%}{%endblock%} {{""}} {% block body_block %}
This Didn't work for me.
The hack I found was to set the vscode language to jinja instead of the auto detected html
reference
I've also just experienced vs-code misbehaving on django template tags (i.e. deleting curly braces).
I don't like the idea of disabling HTML formatting just to support templates (i.e. vs-code Preferences/Settings/Extensions/HTML: disable (uncheck) "HTML>Format:Enable"). This is arguably a step backwards, but it does stop vs-code misbehaving.
Instead, I chose to install (vs-code Preferences/Extensions) the 'Django' extension, by Baptiste Darthenay. This was a better way to go, because it works, gracefully, preserves native vs-code HTML formatting, and includes a nice set of django snippits, which saves me keystrokes when embedding template code. Tada!
BTW, before finding Baptiste's awesome extension, I also tried keeping vs-code HTML formatting enabled, AND enabling 'HTML>Format:Templating', which promised to "Honor django and other templating language tags"; it did not.
I have a template tag named 'string_after' that I want to use within a URL. however I cant find anywhere what the syntax would be to use the tag within a URL?
I would use the tag as such
{% string_after type.site_type 'd' %}
which I tried to put inside the url but it is just seen as another url variable instead of the function
<li> %}">{{ type.site_type }}</li>
Does anybody know the correct syntax?
Thanks
You can't embed one tag inside another like that.
What you can do is save the output of one tag as a variable and then use it in the other:
{% string_after type.site_type 'd' as string_to_use %}
Then:
{% url 'sites:site_list' 'all' string_to_use %}
This assumes that string_after is a simple_tag.
Seems like elementary question, and yet can't get it work
{% if iterator.next > 10 %}
Do smth
{% endif %}
Two issues. First, this code just won't work (the code in the if-condition never implemented even when the condition seems to hold true), and second issue - the ">" sign is highlighted as if it where the closing tag of the closest open tag. Any ideas how to fix the first issue and is it all right with second issues ? Maybe there's some elegant syntax that I am missing and that would remove this ambiguity for the text editor ?
iterator.next may be a string which would result in the statement being False.
Try creating a custom filter to convert it to an int. For example create "my_filters.py":
# templatetags/my_filters.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
Then in your template:
{% load my_filters %}
{% if iterator.next|to_int > 10 %}
Do smth
{% endif %}
More on custom tags and filters here
I wouldn't worry about the highlighting, this may just be your IDE. I recommend using PyCharm for Django development
Django's docs says that you can use > with if tag:
{% if somevar < 100 %}
This appears if variable somevar is less than 100.
{% endif %}
take a look at documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
maybe you are missing something else?
I was able to convert most {% url %} template syntaxes to Django 1.5.
But I'm not able to convert this kind of old url's to Django 1.5:
{% url monthly_archive date|date:'Y' date|date:'m' %}
This does not work:
{% url "monthly_archive date|date:'Y'" date|date:'m' %}
Any ideas?
Best Regards,
Why would you put the close quote there, after the first parameter? That makes no sense. It should go after the URL name, so that the thing quoted is just "monthly_archive".
Django has two template tags "with" and "url". It would be handy to be able to combine the two:
{% with view=really.long.path.to.some.view.somewhere %}
{% url view.foo %}
{% endwith %}
But if you try doing that, you find out that the "with" isn't getting applied inside the "url" tag (as you get an error about there not being a "view.foo").
So, my question is, am I just missing some flag/option/alternative format that would make the above work, or is truly impossible to simplify "url" tags using "with"?
It is possible in Django 1.3 if you're willing to use a future compatibility library.
See the section Forwards compatibility at https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url (just above the discussion on widthratio) for an explanation and examples.