Django - tables2 STATIC URL not working - python

https://django-tables2.readthedocs.org/en/latest/pages/tutorial.html
I followed this tutorial to the point and managed to get tables to display BUT it's not loading CSS file. Instead of this(specified in HTML file):
<link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
it's only showing:
<link rel="stylesheet" href="django_tables2/themes/paleblue/css/screen.css" />
Here is my views file:
from django.shortcuts import render
# Create your views here.
from django.template import RequestContext
from django_tables2 import RequestConfig
from keywords.models import Person
from keywords.tables import PersonTable
def people(request):
table = PersonTable(Person.objects.all())
RequestConfig(request).configure(table)
return render(request, 'people.html', {'table': table})
Any idea what I am missing ?
edit: If I add this in mysite/urls then it loads css in source but displays: "bad operand type for unary +: 'list' error:
+ patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
edit 2, tried a little different way:
STATIC_ROOT = "/root/newproject/mysite/static_files/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_files/"),
)
STATIC_URL = '/static/'
Did this to settings, and I am calling it with:
<link href="{% static 'screen.css' %}" rel="stylesheet"/>
It doesn't do anything, but if I change it to:
{{% static 'screen.css' %}}
Then it loads css but with this error:
TemplateSyntaxError at /keywords/
Could not parse the remainder: '% static 'screen.css' %' from '% static 'screen.css' %'

I used Ubuntu and the file screen.css is in
/usr/lib/python2.7/dist-packages/django_tables2/static/django_tables2/themes/paleblue/css/screen.css
then,
I changed
<link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
for
<link rel="stylesheet" href="/static/django_tables2/themes/paleblue/css/screen.css" />
and it worked for me.
sorry for my English

Did you {% load staticfiles %} before you used this variable?

Related

Django templates not picking static files

Following is the my static folder structure:
ProjectABC
- App1
- App2
- App3
- ProjectABC
- resources
- static
- imgs
- css
- templates
This is how the project structure looks like.
This is how the static configuration in settings.py looks like:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
This is how the base.html looks like:
<!doctype html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1, initial-
scale=1, user-scalable=0">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?
family=Open+Sans:400,600,800">
<title>{{SITE_NAME}}</title>
<link rel='shortcut icon' href= "{% static 'img/favicon.png' %}"/>
<link href="{{STATIC_URL}}css/application.css" media="screen" rel="stylesheet"
type="text/css" />
<link href="{{STATIC_URL}}css/style.css" media="screen" rel="stylesheet"
type="text/css" />
<script src="{{STATIC_URL}}js/application.js" type="text/javascript"></script>
{% block extracss %}
{% endblock %}
</head>
<body>
<div class="container">
<h1>Hello</h1>
</div>
</body>
</html>
All the static files are being loaded as seen in terminal, but in browser its just loading bare html without any css or img:
Please lemme know if i am missing something.
Thnk You!
<link href="{%static 'css/style.css' %}" media="screen" rel="stylesheet"
type="text/css" />
add settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Project App Url
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Static directory must be under the app itself. Create a 'static' directory directly to your app and for better practice, you can also create another directory to the static directory with your app name (static/[app_name]).
Also, {% load static %} must be above doctype in your base.html
change the static config in your settings.py to this:
STATIC_ROOT = os.path.join(BASE_DIR, 'resources/static')
STATIC_URL = 'resources/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'ProjectABC')
]
then run python manage.py collectstatic
and when you call the static file do it like this {% static 'css/style.css' %}

Django 404 css file

For a long time I tried to solve the problem with loading all my static files. When I found a working solution, all svg started loading, but css files didn't.
Here is my settings.py (showing you only main things)
import mimetypes
mimetypes.add_type("text/css", ".css", True)
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = False
STATIC_URL = '/static/'
if DEBUG: STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
else: STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Here is my urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
urlpatterns = [
path('', include('main.urls')),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Here is an example of using css files in my templates
{% load static %}
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-v4-grid-only#1.0.0/dist/bootstrap-grid.min.css">
<link rel="stylesheet" type="text/css" href=" {% static 'css/reset.css' %}">
<link rel="stylesheet" type="text/css" href=" {% static 'css/main.css' %}">
And this is the error in Chrome Console
Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
And also i cant open css files in a new tab. I am getting that error
Also, if you remove %20 from the address bar, then I will open the css file
P.S.
I am trying to deploy it
1- Try to replace a slash forward path
<link rel="stylesheet" type="text/css" href=" {% static '/css/reset.css' %}">
2- whitenoise
pip install whitenoise
Then, set it to "MIDDLEWARE" in "settings.py". Finally, CSS is loaded successfully:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", # Here
# "whitenoise" will solve "MIME type" error then CSS is loaded successfully:
]
3- change your static URL like that:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I think your mistake is that you put in a space in front of your static link
<link rel="stylesheet" type="text/css" href=" {% static 'css/reset.css' %}">
Try removing the space in front of {% so it will become
<link rel="stylesheet" type="text/css" href="{% static 'css/reset.css' %}">
%20 is translated to a space character in URL's, so that why I think that's the issue

Link static/css file with Django template

I am pretty new to Django and I am trying to link CSS file with my HTML document and I have tried using all possible ways posted on StackOverflow and I am unable to locate what is the issue here.
Here is the directory and file structure:
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
index.html
{% load static %}
<link rel="stylesheet" type="css/text" href="{% static 'css/style.css' % }">
Thanks in advance.
Ops. Silly mistake. It was all about indentation and minor type tag changes. Here is what worked out for me.
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

Cannot resolve symbol Static

{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'search/style.css' %}">
This is the code and I am getting error in loading static file in this part
href="{% static 'search/style.css' %}
error cannot resolve directory "{%static search
Are your settings pointing to that (search/style.css)directory? You'll need something like the following in your app's settings.py:
...
STATIC_URL = '/search/'
STATIC_ROOT = os.path.join(BASE_DIR, 'search')

Not found my staticfiles in django 1.9

I have a little problem with Django , it is not my static files.
I have read the documentation , I tried putting the path directly , use {{ STATIC_URL }} and most recommended method is to leave the details. Any idea how to fix it ?
template.html:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/normalize.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/component.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-select.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-skin-boxes.css' %}" />
local.py (settings)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR2.child('static')]
my file folder in the right direction
The first folder is the virtualenv, the second folder is for Django Project where is the manage.py (image)
Views, models, urls and forms working properly, and display the chosen template
[The python code working properly (image)][2]
Update
I try change STATICFILES_DIRS for this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', ),
)
And for and printed the path to make sure it is correct, but the the error continues.
The result of print
Thanks in advance
I've resolved for Django 1.9 is: STATICFILES_DIRS = (BASE_DIR, 'static')
To start, I would make STATICFILES_DIRS a tuple.
Next, I would try a format like this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs'),
)
To make sure it is going to the right directory, print the path and adjust accordingly:
print os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs')
Hope that helps!
I have solve that problem on mac by using:
STATICFILES_DIRS = [os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),]

Categories

Resources