Keyboard shortcut for {% %} in VSCode using Django HTML - python

I am building my first Django app and using the Django templating engine in my html files. I have the html and Django html plugin in VSCode.
So far, it autocompletes html elements and colorizes the Django templates.
Is there a way to autocomplete {% %} when using the Django HTML language mode in VSCode?

You must add configuration below to your VS Code settings to get extention fully wotking, and your file must be in folder named templates or one of his subfolders, if you want your templates to be in a different folder, you change the settings to according to your configuration
"files.associations": {
"**/*.html": "html",
"**/templates/**/*.html": "django-html",
"**/templates/**/*": "django-txt",
"**/requirements{/**,*}.{txt,in}": "pip-requirements"
},
"emmet.includeLanguages": {"django-html": "html"},
you can find more detail in the extension GitHub page

You can you the django-Intellisense VSCode extension to provide autocomplete for all dynamic properties generated by Django.

Related

What does request_source do in a Django template

I am picking up some old Django code and am puzzled by a line in the site's master template.
After a series of {% load xxx %} lines there is the line
{% request_source 849051 %}
I have been unsuccessful in finding any documentation for this command. Is it a standard template command? Or is it something custom to this code, and if so where would I likely find the implementation?
The site was written for Django 1.5.12 if that makes a difference.
{%load ... %} loads various python routines from the templatetags directory of an installed django app (in installed_apps in settings.py) think of this like import package but you are limitted to packages only available inside the templatetag folder of an installed app
{% funciton_call arg1 arg2 ... %} is calling a function defined in the python files in the templatetags directory
request_source is not a django thing, it is a function in one of the templatetag files

How to properly add template tags to a django project

I am playing with django-gentelella and try to add custom template tags to the project.
According to the latest Django documentation, one should add a "templatetags" directory, at the same level as models.py, views.py, etc. Also, an init.py file should be placed in the directory.
I added my template tags into a file called "template_tags.py" and restarted the server. In my templates, I load the file using "{% load template_tags %}" at the top of the file.
Unfortunately, this does not work yet. According to the Django documentation, it is also required to add the template_filters to the INSTALLED APPS.
My problem is that I cannot figure out how to get the right path in dot notation. Can anyone point me in the right direction?
/profiles/templatetags/custom_tags.py
#register.filter(name='getLocalTimeDifference')
def getLocalTimeDifference(value):
value = value.replace(..)
return value
/profiles/templates/navbar.html
{% load custom_tags %}
...
<div class="notification-meta">
<small class="timestamp">{{ notification.timesince | getLocalTimeDifference}} before </small>
</div>
P.S. __init_.py should also be added to templatetags directory

Setting up Flask-Admin and Flask-Security

I have a Flask-Admin project set up with Flask-Security as well. It is pretty much https://pythonhosted.org/Flask-Security/quickstart.html#id1 but just more advanced. I can access the login page at localhost/login and logout and localhost/logout. The logging in and logging out works.
The templates for Flask-Admin works and everything is displayed as I'd expect. However, there are no templates on my machine or docker container where the Flask-Admin app is run. I installed Flask by running pip install Flask-Admin. I know I can over ride the security log in by adding something like
SECURITY_LOGIN_USER_TEMPLATE = 'security/login_user.html'
to the config file and uploading to /templates/security/login_user.html. There is also using
{%extends base.html}
to have a common theme. Should I have template files already in my project?
Flask Security have a default login template, if you want to use your own template for login or register follow these steps:
Create in template folder the a subfolder named security
Add your html documents to this folder
Go to your flask configuration and add the following settings:
If your want the register functionality
SECURITY_REGISTERABLE = True
Add the name of your templates:
SECURITY_LOGIN_USER_TEMPLATE = 'security/login.html'
SECURITY_REGISTER_USER_TEMPLATE = 'security/register.html'
Remember to use the appropriate form in login.html and in register.html, usually causes doubts but is simple:
register.html: register_user_form.field
login.html: login_user_form.field
These are the configurations for this work correctly.
this repository can you to see and understand better doubt:

Filepath for Django Extending Template

Here is my template path
webapp/
|__templates/
|__frontpage/
| |__home.html
|__home_base.html
In home.html, it has:
{% extends "home_base.html" %}
This file structure will work. However, I want to put home_base.html inside frontpage/, as this makes more sense. However, Django will report home_base.html TEMPLATE DOES NOT EXIST, if home_base.html is moved to frontpage/.
The error says it cannot find the home_base.html file under templates/ folder. Since the home_base.html is moved to frontpage/, why doesn't it search for home_base.html inside frontpage/ first? Any configurations I am missing?
You need to do the following for template to be extended.
{% extends "frontpage/home_base.html" %}
Django does not have an idea where you have have moved your template. It will look for the template according to the templates path you have defined in your settings.
The template loader will look for template in the directories defined in the DIRS setting in the TEMPLATES settings.
From the DIRS setting documentation:
Directories where the engine should look for template source files, in
search order.
Also, if frontpage was an app and you had placed your template in the frontpage app instead of the templates folder, then you can set the APP_DIRS settings to be True. This will tell Django to find the templates in the individual apps also.
From the APP_DIRS setting documentation:
Whether the engine should look for template source files inside
installed applications.

How to combine Django with Jade

I'm trying to combine Django with Jade, but I've had some problems.
I have model which is named About. This has a view like this:
def about(request):
return render_to_response('about.jade',{},RequestContext(request))
and in my urls I have:
url(r'about/', views.about),
But it provides an error that the Templates doesn't exist (and yes, it exists). Is it correct to write the url like this?
Any help would be appreciated!
If your getting the big Template does not exist page in your browser, this usually means that django cannot find where you have stored the your template file (irrespective of using jade).
If you ve created a djnago 1.6 project you need to add the following line to settings:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Then create a templates directory inside your app (not project) directory, and put your template there.

Categories

Resources