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
Related
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.
I've referred extensively to some of the great answers at Django: 'current_tags' is not a valid tag library , but can't solve the problem with any permutation of the suggested solutions.
Here's the vanilla structure in my project folder:
templatetags
-> __init__.py
-> custom_tags.py
I have the line {% load custom_tags %} in my html file, which extends base.html.
In settings.py, "project_name" is included in INSTALLED_APPS. Like mif suggests, I try python manage.py shell. Without altering settings, I get ImportError: cannot import name 'custom_tags'.
If I add the entry project_name.templatetags.custom_tags to INSTALLED_APPS, then my aforementioned attempt to launch the shell fails with two exceptions: AttributeError: module 'demos.templatetags' has no attribute 'custom_tags', and ModuleNotFoundError: No module named 'demos.templatetags.custom_tags'.
custom_tags.py is quite simple:
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
#register.filter
def str_concat(a):
return str(a) + " eyy"
#register.filter('str_concat', str_concat)
I've run the file (with python custom_tags.py) with that last line commented and uncommented. I've also touch 'd both custom_tags and init. I've also relaunched the dev server numerous times (via ctl+c and python manage.py runserver). Also, I've confirmed that the two imports in custom_tags work fine and are successfully installed in the venv.
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
I am using WebPack to setup Django and React. So far, I have generated a bundle and I am trying to reference it in my template:
{% extends "main/base.html" %}
{% load render_bundle from webpack_loader %}
{% block main %}
<div id="App1"></div>
{% render_bundle 'vendors' %}
{% render_bundle 'App1' %}
{% endblock %}
However, Django can't seem to find the correct bundle files and I get a 404 error shown in the diagram below. Did I initialize everything correctly in the settings.py?
EDIT:
webpack-stats-local.json: # Generated correctly from config files
{"status":"done","chunks":{"App1":[{"name":"App1-a1f17f437b3aacf3188f.js","path":"/Users/andyxu/Documents/my_website/my_website/static/bundles/local/App1-a1f17f437b3aacf3188f.js"}],"vendors":[{"name":"vendors.js","path":"/Users/andyxu/Documents/my_website/my_website/static/bundles/local/vendors.js"}]}}
print(STATIC_ROOT)
==>
/Users/andyxu/Documents/my_website/my_website/static
I am not an expert in django but you can simply call your vendor and App1 files with script tag.
vendor and App1 files are the only required code to load react appropriate data.
In your case check if you are accessing path to javascript files correctly. try to provide absolute path to vendor files and see if it loads?
Your settings look normal, but those 404 indicate that Django can't find the static files, even though they're there.
The most likely cause is some path misconfiguration. While staticfiles.views.serve should consider all STATICFILES_DIRs, I'm not exactly sure it does. Either way, try to define STATIC_ROOT = os.path.join(BASE_DIR, 'my_website', 'static') explicitly.
Also, add a temporary print(STATIC_ROOT) (or use debugger) to see the actual path - maybe your BASE_DIR is off. It's very easy to miss a directory level there.
Oh, and from your screenshot it seems that there is no webpack-stats-local.json in bundles/local/. Check your BundleTracker confuguration, it should approximately look like this:
new BundleTracker({
path: path.join(__dirname, 'my_website/static/bundles'),
filename: 'webpack-stats-local.json'
})
Without a proper path there it'll silently fail. Just had this issue the other day - no warnings whatsoever, just the file wasn't anywhere to be found.
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.