Django Template Variables in static path - python

I have a template where I need to access a directory based on the user's fields.
The code looks like this:
{% with user.client.company as company %}
{% static 'uploads/{{ company }}' as files %}
{% endwith %}
I also tried:
{% static 'uploads/{% user.client.company %}' as files %}
How can I achieve this? I want the folder name to be equal to user.client.company but right now I get an error like:
Invalid block tag: 'static', expected 'endwith' for the first one and
Invalid block tag: 'static' for the second
Obviously I am doing it wrong. Any help would be much appreciated.
It was pointed out I should use media rather than static. But even so, I am unsure how to dynamically set the path based on the user's parameters.

remember to load you statics!!
{% load staticfiles %}

Related

Django, following a tutorial and there's an issue where my {% block content %} is not being recognized

Here's the dir structure:
directory structure
If the image isn't displaying here's a text version:
templates
myapp
new_search.html
base.html
base.html code:
{% block content %}
{% endblock content %}
new_search.html code:
{% extends "base.html" %}
{% block content %}
<h2>NEW SEARCH</h2>
{% endblock %}
I can display the base.html fine.
The new_search.html however displays like this:
new_search.html
I also want to mention a side question. My Django server isn't running however I can still open the html in my browser. Is that supposed to happen? My URLs when opening both html's are:
base.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/base.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
new_search.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/myapp/new_search.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
Also, i'm getting this error when trying to visit my home page:
error screenshot
EDIT:
Adding more info. I now get this error. And my new_search template still isn't including the base template. I tried moving the html to different directories etc. In the tutorial it matches too.
template Doesn't Exist
it says the source doesn't exist but I can see it right there. proof
You might want to try copying "base.html" in templates --> myapp folder.
Basically maintain both hmtl files in the same directory.
And also try giving full path after copying them in same dir, like:
{% extends "myapp/base.html" %}
For the home page, you need to have the tag {% load static %} to be able to use the static tag.
Make sure you are using the render() function in your view so it doesn't just return the template as text
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

HTML changes not reflected in django-cms

I was working on an old django-cms project and was trying to edit base.html file and no changes are reflected when I reload the page.
But if I delete all the lines in that file the django runserver refuses to start showing error
django.core.exceptions.ImproperlyConfigured: The 'js' and 'css' sekizai namespaces must be present in each template, - or a template it inherits from - defined in CMS_TEMPLATES. I can't find the namespaces in 'project/cms/home.html'.
They why isn't other changes like adding a new class not reflected in the page reload or server restart.
NOTE:
The project is working good as it is. I was trying to modify it a little bit. Changes I made in the css pages are getting reflected when I reload the page. Issue is only when I try to edit HTML pages
For base.html, you need to have {% load cms_tags sekizai_tags %} in the file. Add {% render_block "css" %} to <head></head> and {% render_block "js" %} somewhere between <body></body>. Depending on the template files that inherit from base.html, certain portions may have been overwritten. For example, if you had:
{# base.html #}
{% block content %}
<div class="example-class"></div>
{% endblock %}
But in another file say:
{# layout.html #}
{% extends "base.html" %}
{% block content %}
{% endblock %}
The div would not appear.
If however, you are talking about missing CSS files, you still need to include them in <head> for it to be displayed. render_block "css" is for django-cms css files that are included in plugins etc. I usually use a LESS or SCSS compiler to include CSS into my projects.
Hope that helps. Post more details for a better diagnosis.

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

Django-cms placeholder in cms template breaks page

So I moved my cms templates to a /cms folder in the templates folder:
templates/cms/default_template.html
I edited the settings.py as follows:
CMS_TEMPLATES = (
('cms/default_template.html', 'Default Template'),
)
My template looks like this:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder template_1_content %}
{% endblock %}
Problem is it throws the following error on page load:
TemplateDoesNotExist at /en/
Error during template rendering
3
4 {% block base_content %}
5 ***{% placeholder template_1_content %}*** -> This line is red indicating the problem is here
6 {% endblock %}
If I remove the {% placeholder template_1_content %} the page loads fine no problems.
It's only after adding that placeholder that it goes bonkers for no reason I can think of.
Has anyone seen something like this before?
BTW the page will load with the placeholder if I don't use the /cms folder.
So what is it about the placeholder and the folder location???
Any help would be appreciated.
I figured it out. Turns out the name of the first template created was stored in the database and that was being referenced. I had to manually edit the database to the new location and name of the template

Django Custom Include Template

I know this isn't exactly Django templating philosophy, but i'd like to be able to include different templates depending on a list of plugins that I've specified in the http response context. For example, if I have the following plugins configured:
context['plugins'] = ['weather']
I attempt to include each template in the base template file:
{% for custom_plugin in custom_plugins %}
{% include "map/plugins/custom/{{ plugin }}/includes.html" %}
{% endfor %}
I've also tried:
{% for plugin in plugins %}
#register.inclusion_tag("map/plugins/custom/{{ plugin }}/includes.html", takes_context=True)
{% endfor %}
For now the each plugin will only contain script references and css classes in their includes.html file:
<script type="text/javascript" src="{{ MEDIA_URL }}map/js/plugins/custom/weather/weatherStation.js?ver={{ version }}"></script>
Any suggestions?
Your first way seems the best, and this answer might provide some pointers as to how you'd go about it: How to concatenate strings in django templates?
You basically want to build a string of the template to include in a variable with the with tag, then include it.

Categories