VSCode: Auto Format on Save - django

I use last version of VSCode, I am on Windows10.
When I do the following:
{% extends 'base.html' %}
{% block content %}
<h1>Test About section</h1>
{% endblock %}
The code is automatically auto formatted when I save:
{% extends 'base.html' %} {% block content %}
<h1>Test About section</h1>
{% endblock %}
I am trying to deactivate it, but I have been unable to. This is what I've tried:
CTRL + Shift + P: 'Disable all Extensions' and also 'Disable all Extensions in Workspace'
In the Settings, the "editor.formatOnSave" is set to false (I have checked in User Settings, Settings for the Workpspace, Settings for Workspace JSON, etc)
Disabled Jinja and Prettier
Even tho I disabled my Extensions, when I hit [Save], the code is automatically formatted.
I am not sure where the settings get imported.
The project is new, I use Django, it's not linked to Git as well.
What I am doing wrong? I've been reading articles for the past hour but the issue keeps occurring, did I miss a setting or does a hidden setting get imported somewhere?

Related

How to indent Django templates properly

I work in SublimeText 3. When writing Django templates I have a mixture of html and functions.
I like to indent my code so that block, if and other such statements are indented. For example:
Manual formatting
{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
{% endblock %}
However, when I run any autoformatter HTML-CSS-JS-Prettify it ignores these brackets and treats them as text:
After formatting
{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
{% endblock %}
Although plugins like Djaneiro give great tag highlighting, I haven't been able to find a way to get SublimeText to treat these as tags.
Has anyone had any luck?
This is a late answer, but I would like to mention a Django template formatter that I've created myself: DjHTML. You can install it using pip install djhtml.
Let's say template.html contains the following:
{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
<script>
$(function() {
console.log("Password changed!");
});
</script>
{% endblock %}
Then running djhtml template.html will give the following output:
{% extends "accounts/base.html" %}
{% block content %}
<h1>Password changed</h1>
<p>Your password was changed.</p>
<script>
$(function() {
console.log("Password changed!");
});
</script>
{% endblock %}
It's easiest to use DjHTML as a pre-commit hook, so that templates will be automatically indented when you run git commit. Instructions on how to configure pre-commit can be found in the README.
There isn't one for sublime text as far as I can tell. I have no source I can quote on this, but I have basically searched nothing came up.
This discussion is by any means old, but active. I found this really old ticket about formatting standards for Django and it has been updated 9 Months ago to basically say they are "in favour of standards" and the proposed formatting for templates would be:
<ul>
{% for x in y %}
<li>{{ x }}</li>
{% endfor %}
</ul>
They also made a place happen that holds information about formatting guidelines in Django.
You might find this discussion interesting as well. It's old too, but it highlights the confusion about formatting in Django and the DIY solutions people came up with to cope.

Django nested templates

In trying to keep with DRY, I'm setting up my Django project HTML files now. I have successfully extracted most repeated information to my base.html page. On most (but not all) of my pages, my content is displayed within a general 'panel' which is basically just a container set-up with styling, but it's got a few div-tags to it so it looks a bit ugly and I'm having to type out the exact same code out several times on each page.
My idea was to extract this to a 'panel.html' then call it whenever I need it, for example some pages might just have one 'panel' whereas my dashboard (it's an administrative site) will have maybe 15+. So it seemed a better idea and cleaner to not have to type out all this code each time I need to set up a 'panel'.
My ideal page would look something like..
{% extends 'base.html' %}
{% block content %}
{% extends 'panel.html' %}
{% block panel_content %}
Panel content...
{% endblock panel_content %}
{% extends 'panel.html' %}
{% block panel_content %}
Second panel content
{% endblock panel_content %}
{% endblock content %}
I know I can't use extends multiple times but I'm using it just as an example for what it is I'm trying to achieve.
I am going to potentially have hundreds of these identical 'panels' across my site but each containing different content and it would be so much cleaner if I could just have one stored somewhere in a HTML file and call it however many times I need.
Is there a way to do this?
You can use include
{% include "panel.html" %}
I should mention that too many include statements create a performance issue.

Unable to extend Django 2.1 password reset template

I'm trying to extend the password reset form like so:
<!-- templates/registration/password_reset_form.html -->
{% extends registration/password_reset_form.html %}
{% block content %}
<h1> hello </h1>
{% endblock %}
As far as I can tell, this should take the template from /usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/registration/password_reset_template.html (which exists, I checked) and replace the block content with the one at templates/registration/password_reset_form.html.
But this isn't happening. In fact, there is no change, nor is there an error. What am I doing wrong?
UPDATE:
I tried deliberately introducing a syntax error into the template name, and no error was issued. It leads me to believe that the template file is not being read at all. According to the django docs, registration/password_reset_form.html is the path to the default template. Why am I not able to at least introduce an error?
Put quotes around path, like this:
{% extends "registration/password_reset_form.html" %}
{% block content %}
<h1> hello </h1>
{% endblock %}
Also if your own template file didn't read at all, make sure that you have your app in INSTALLED_APPS in settings.py and TEMPLATES has 'APP_DIRS': True or DIRS set properly.
Why don't you just use the full path to the html file then?
/usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/registration/password_reset_template.html
Right now you are extending the same file you are working on

Visual Studio Code and Flask Jinja Templates

I have recently switched to Visual Studio Code (previously used PyCharm) and am now facing a formatting problem.
With the following example html:
{% extends 'base.html' %}
{% block body %}
{{ super() }}
<div>Something</div>
{% endblock%}
after saving (I have turned on formatOnSave) I get the following:
{% extends 'base.html' %}{% block body %}{{ super() }}
<div>Something</div>{% endblock%}
I am using the editorconfig plugin and have stayed with the default user settings.
How can I configure VSC to not force Jinja blocks to be on a single line?
There's a known issue with trailing spaces.
You might try another extension dedicated to jinja: https://github.com/wholroyd/vscode-jinja
I have a temporary solution for that.
Use Better Jinja extension and add to jinja2 files .html.j2 extension.
You lose build-in HTML formatting but preserve jinja2 syntax :)

django-cms menu not showing

I am trying to run a project in which header has some menu links which are not loaded. The code to load the menu is as follows
<ul>
{% load menu_tags %}
{% for child in children %}
<li><a {% if forloop.first %} class="first" {% endif %}href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a></li>
{% endfor %}
</ul>
It looks that the for loop is not running as the children array is empty. I am not getting the idea where to look for this children array. From where this array gets loaded. If django-cms query the database then what columns does it query.
The project was actually built on some older version of django 2.1. Now I am trying to run the project on django-cms 3.0.7. I am using mysql database and have imported the data.
As well as importing the data, you need to do some additional work.
See:
http://docs.django-cms.org/en/support-3.0.x/upgrade/2.1.html
http://docs.django-cms.org/en/support-3.0.x/upgrade/2.4.html
http://docs.django-cms.org/en/support-3.0.x/upgrade/3.0.html
There are some steps there that you can't leave out. In particular, see the notes in that second document about migrations, and about the cms moderator command.
Don't do anything without first making a database backup.