I am getting a 404 error with my static files in production. The weird thing is that django-rest-framework and admin put there static files in the correct directory that I set up. I am using Digitalocean to host my site.
Directorys:
-django_project
- django_project
- templates
- media
- static
- apps etc
settings.py:
SATICFILES_DIRS = (
'/home/django/django_project/static/',
)
STATIC_URL = '/static/'
STATIC_ROOT = '/home/django/django_project/static/'
urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
base.html:
{% load static %}
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Application</title>
<link href="{% static 'css/styles.css' %}" rel="stylesheet">
</head>
Try changing your urls to this:
if settings.DEBUG:
urlpatterns += patterns('',) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('',) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You need to include the initial url patterns.
Related
I stored my static files in project root directory but I can't make them load on the browser.
configurations
settings.py
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIR = [
STATIC_DIR,
]
template file
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Index</title>
</head>
<body>
<h1>{{temp}}</h1>
<h2>Click here to veiw user details</h2>
<img src="{% static "user/images/smile.png"%}" alt="oh oh didn't show"/>
</body>
</html>
In the file urls.py add the static url.
urlpatterns = [
path('admin/', admin.site.urls),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I'm having alot of trouble either understanding how to properly host my static files on both local and heroku. Currently when Debug is turned off, I get an error 500- otherwise it works fine.
I've read quite a few SO posts but so far, no solution has helped. I've also tried to host staticfiles on AWS, but this has still not helped.
Edit
I currently get a 500 error on any page but the admin console.
Error from django server:
[27/Nov/2019 13:36:07] "GET / HTTP/1.1" 500 9061
Error from web console:
Failed to load resource: localhost1/: the server responded with a status of 500 (Internal Server Error)
Below is my code:
settings.py
INSTALLED_APPS = [
'widget_tweaks',
'django_select2',
'masterform',
'tempus_dominus',
'team',
'sms',
'opportunities',
"rest_framework",
'import_export',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, "live-static", "static-root")
urls.py
from django.views.static import serve
from bdsurveyapp import settings
urlpatterns = [
url('', include('opportunities.urls')),
url(r'^sms/', include('sms.urls')),
url(r'^admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
url(r'^dashboard/', include('dashboard.urls')),
path('login/', auth_views.LoginView.as_view(template_name='pages/login.html'), name="login"),
path('logout/', auth_views.LogoutView.as_view(template_name='pages/logout.html'), name="logout"),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
partial base.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
{{ form.media.css }}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="apple-touch-icon" sizes="180x180" href="{% static 'img/apple-touch-icon.png' %}">
<link rel="icon" type="image/png" sizes="32x32" href="{% static 'img/favicon-32x32.png' %}">
<link rel="icon" type="image/png" sizes="16x16" href="{% static 'img/favicon-16x16.png' %}">
<link rel="manifest" href="{% static 'img/site.webmanifest' %}">
<link rel="mask-icon" href="{% static 'img/safari-pinned-tab.svg' %}" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
I had a similar issue and I found that when I commented out 'django.contrib.staticfiles' in INSTALLED_APPS my issues were fixed.
Well, I am trying to make a simple app in django in which I upload files from admin panel. The user is greeted with a simple search page in which he enters an id which thus needs to produce the related file. The file is to be in pdf format. So if there is a way I could view the pdf file in the browser and a download button which downloads the file. The code is hosted on Github, the link is https://github.com/tahseen09/delhipatho
Add STATIC_ROOT = '/static/' to your settings.py
urls.py
urlpatterns = [
path('',views.index, name='index'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
from report.models import query
def index(request):
if request.method == "POST":
form = Form(request.POST)
if form.is_valid():
query = query.objects.filter(id__exact =form.cleaned_data['id'])
return render(request, 'ans.html', {'query':query})
else:
form = Form()
return render(request,"index.html", {'form':form})
ans.html
{% load static %}
<!DOCTYPE HTML>
<html>
<head>
<title>Delhi Patho</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="{% static 'assets/css/main.css' %}" />
</head>
<body class="is-preload">
<p><a href="{{query.repo.url}}">Download</p>
<!-- Scripts -->
<script src="{% static 'assets/js/main.js' %}"></script>
</body>
</html>
I cannot get the CSS to appear when I refresh the page nor when I turn the server off and then restart it.
{% load staticfiles %}
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name='viewpoint' content='width-device-width, initial-scale-1.0'>
<meta http-equix='X-UA-Compatible' content='ie-edge'>
<link rel="stylesheet" type='text/css' href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css"/>
<link ref="stylesheet" type='text/css' href="{% static 'css/styles.css' %}" />
My settings.py file has STATIC_URL = '/static/'
I'm not sure what else it could be. I've attempted several other stackoverflow questions but I'm out of ideas.
Any help is appreciated.
in setting.py file your set DEBUG=True, please update url.py file root (url.py project)
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#url pattern.....
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and than if DEBUG=False ALLOWED_HOSTS=['your_host'], please running command ./manage.py collectstatic from the terminal
Maybe in your urls.py you did not import from django.conf import settings and/or
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
and you did not add urlpatterns += staticfiles_urlpatterns() in your urls.py too.
And maybe your first css (I mean <link rel="stylesheet" type='text/css' href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css"/>)
Blocks your second css. Try to comment your first css and check is it working.
I am using Django 1.8 for my project but the problem is my static files are not loading into the template. I have followed each steps in detail, but still don't know why it is not loading.
Template file
{% load staticfiles %}
<!DOCTYPE html>
<html>
<!-- This code is only meant for previewing your Reflow design. -->
<head>
<link rel="stylesheet" href='{% static "css/boilerplate.css" %}'>
<link rel="stylesheet" href='{% static "css/index.css" %}'>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0">
</head>
<body>
Settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'staticfiles')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media')
STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static'),
)
Project Directory
Could anyone tell me why the static files are not loading? The page source in browser is accurately showing the file path. When i click on it, it is showing me the css content but it is not loading when I run the server.
Page Source:
<!DOCTYPE html>
<html>
<!-- This code is only meant for previewing your Reflow design. -->
<head>
<link rel="stylesheet" href='/static/css/boilerplate.css'>
<link rel="stylesheet" href='/static/css/index.css'>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0">
</head>
<body>
urls.py file
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', AboutView.as_view(), name='index-template'),
url(r'^register/', RegistrationListView.as_view(), name='register'),
url(r'^form/', RegistrationForm.as_view(), name=''),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Inspect Element Result:
You are missing an 'S' in the spelling of STATICFILES_DIRS.
Once you correct that, do a python manage.py collectstatic and restart the dev server.