loading staticfiles in django template causes server error 500 - html

I have a base.html file extended by the index.html one. In base I load some static files in the section, and other in the bottom of the file.
I am trying to use the template tag for loading static files:
{% load static %}
However, depending where I put it, this works or fails.
When the tag is on the begining of the base (first line), this always does not work.
It works only when it is on the body section of the base.html and on the begining (just after extend tag) of the index.html.
Where is the correct place to put it ? Should it be called only once ?
EDIT:
DEBUG is set to False. There is no problem when it is set to True.

Related

Include and Static statements are not being recognized with GitPages

I am new to GitPages and have successfully built and ran my page. However, the {% include %} and {% static %} methods in my index.html aren't being registered. As you can see here. Additionally, my two static files referenced in this file (circleselfie.png and home.css) have weird characters in their static paths when inspecting the page. Here is my project: https://github.com/jishli113/JSite/tree/master. What is causing this?
Gitpages, unfortunately, doesn't support a django backend. It's primarily for static/clientside sites, so your templates are just being treated as normal HTML.

Problem with CSS styling in a html page in Django

So, i am literarily one week into learning web development and i'm following this Django tutorial, i got the index page that i swiped from online to compile properly with it's static elements also. Ran the "python3 manage.py collectstatic" command and everything.
So now i'm linking a registration page which i setup up as a new app in the project and when i try and put the css styling file it doesn't work. i've tried just puting in the same directory as the html templates, then i've moved it to styles and rerun the earlier python command so it's also present in assets, also made use of the "{% static 'abcde.css' %}" as well as {% load static %} in the register.html (only did it in the index initially) and i'm still having no luck with styling the page, any help would be appreciated
Edit:
I previously said i moved the css file but i meant the static file and run gatherstatics again so theres a copy in assets as well.
So now i'm linking a registration page which I setup up as a new app in the project and when i try and put the css styling file it doesn't work.
You should not put it in the same directory as the templates, nor in the directories for templates at all. It should be in a directory you use for static files, so the path is relative to the static root.
This thus means that if /static/ is the directory you marked with the STATIC_ROOT settingĀ [Django-doc], and the file is located at /static/foo/bar.css, you use {% static 'foo/bar.css' %}.
I figured it out but I don't think I should be deleting my stack overflow question. It was just a simple syntax problem
what i had: {% static 'styles/style.css' % }
what you need: {% static 'styles/style.css' %}
that gap between the percentage and bracket was the problem

How can I include all my html files in my base.html file in django?

I have a couple of html files in my django project including a base.html file, navbar.html, footer.html, etc. I have included the navbar and footer files in the base file and extended the base file to a home.html file which happens to be my main page. I recently created a courses.html file and would like this page to also be in the main page. I understand I can include it like I did with navbar and footer, but that would mean the courses.html file will be shown everywhere the base file has been extended to, and I don't want this. How can I do this?
If you include courses.html only in home.html then it will be seen only in view that home.html is a template in.
Anything you include in base.html will be seen at every page which uses {% extends 'base.html' %}.

Django static files being generated with additional path

So, I'm using Django and pythonanywhere in order to setup an webpage, but I'm facing some issues when retrieving static files.
I've followed the tutorial for static files (which is great btw) and everything works for the index.html page. However, when trying to go to a second page, it throws an error which fits one of the issues described in this other link: https://help.pythonanywhere.com/pages/DebuggingStaticFiles/
The path to the file and the path in the URL don't quite match (eg, there's an extra level of folder hierarchy in one and not the other)
However, I don't know how to fix it... What is happening is that my log info shows that I'm loading:
/static/pgbfiles/medicine.png -- this is working for the index.html
/publicacoes/static/pgbfiles/img.css -- for the second page named publicacoes.html
What I would like to have is all the static files in one single folder. They are already there but I don't know how to tell Django that they are all there for both html files...
I think I could add an additional folder for the "publicacoes" files, but that is not what I would like to do atm.
And this is how I'm loading the info on both index and publicacoes.html:
{% load static %}
And then on the particular places where I need: href="{% static 'pgbfiles/bootstrap.min.css' %}"
Thank you for any help!
EDIT:
index.html is using: {% static 'pgbfiles/bootstrap.min.css' %} and working;
publicacoes.html was using {% static 'pgbfiles/img.css' %} but I changed it to {% static '/pgbfiles/img.css' %} and it is still not working -> still requests for "GET /publicacoes/static/pgbfiles/img.css".
Static info are:
STATIC_URL = 'static'
STATIC_ROOT = '/home/guideo/pgbadvogados/blog/static'
WORKING:
Now I've changed STATIC_URL to '/static' and it worked! So thanks a lot for the two comments and answer down below.
(only had to change it on settings.py, and not on publicacoes.html as I was doing before).
You're specifying a relative path (no preceding forward slash), so the request is going relative to the location of the page that contains it. Add a preceding slash to your href.

Django translations inside included HTML template

I am including a file in a Django webpage using this:
{% include 'footer.html' %}
This included HTML file contains trans tags, which are not being translated. In fact, Django returns a TemplateSyntaxError:
Invalid block tag: 'trans'
I know that the include tag does not process the included file. I've seen other questions ask about overriding blocks in included files, to which the answers mentioned that this is not possible in Django. I presume this is the same problem.
Is there a way to force Django to process included files, or some other tag that achieves this? Or maybe Python code, as a last resort?
Important note: I cannot simply add the footer in a base file and extend from this base, because I already have a base, which is actually the one trying to include footer.html (I have several base templates for different sub-sites, but all of which should use the same footer).
inside footer.html you need to load i18n
{% load i18n %}
quote from the docs:
As with all template tags, this tag needs to be loaded in all
templates which use translations, even those templates that extend
from other templates which have already loaded the i18n tag.