django image can not be seen - django

I followed the steps indicated in the tutorials but the image is still not seen. It finds it but it does not show it: "GET /static/imagenes/triste.png HTTP / 1.1" 404 1782. The images are in static/imagenes.
{% load static %}
<img src= "{% static 'imagenes/feliz.png' %}" alt="hola" >
settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR,"static")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('configuracionSecreta/', include('apps.configuracion.urls')),
path('dialogo/', include('apps.dialogo.urls')),
path('posicionamiento/', include('apps.dialogo_pos.urls')),
path('resultados/', include('apps.resultados_test.urls')),
path('', include('apps.test_datos.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
├───.vscode
├───apps
│ ├───configuracion
│ ├───dialogo
│ ├───dialogo_pos
│ ├───resultados_test
│ └───test_datos
├───media
├───static
│ ├───bower_components
│ ├───imagenes
├───templates
│ ├───base
│ ├───configuracion
│ ├───dialogo
│ ├───dialogo_pos
│ ├───resultados_test
│ └───test_datos
└───tfg_project
#dialogo_pos/urls.py
from django.urls import path
from apps.dialogo_pos.views import *
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('', dialogoPosIni.as_view(), name='inicio'),
path('final', dialogoPosFin.as_view(), name='fin')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

Static Files not loading when DEBUG = False

I'm using namecheap shared hosting and I hosted my site using cpanel. My site is working fine but if i made DEBUG = False in project settings.py file the static files are not loading. My site url: https://drshahidabegum.com/
settings.py
-----------
# Static files (CSS, JavaScript, Images)
STATIC_DIR = [
BASE_DIR / "static",
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py in project folder
-------------------------
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dr_shahida.urls')),
]
# Configure Admin Titles
admin.site.site_header = "DR. Shahida Begum"
admin.site.site_title = "DR. Shahida Begum"
admin.site.index_title = "Welcome to Admin Dashboard"
handler404 = 'dr_shahida.views.error_404_view'
urls.py in app folder
-------
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name='index'),
path('testimonial/', views.testimonial, name='testimonial'),
path('contact/', views.contact, name='contact'),
path('blog/', views.blog, name='blog'),
path('blog/post/<int:post_id>', views.blogdetailview, name='blogdetailview'),
path('set_language/<str:ln>', views.set_lang, name='set_lang'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I want my static files to work when DEBUG = False in project settings.py file.

Refused to apply style because its MIME type ('text/html') in django and next js

hello i did everything with integration but nothing seems working,its again saying the same error
all next js files are default no changes
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'frontend/.next/static/css')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.test)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
pls help me find a solution to this problem i have done everything that i can do with this integration

Favicon redirect in django 2.0

I'm trying to redirect the default browsers request of /favicon.ico to serve the picture from my static folder and I get a 404(server log screenshot).
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
urls.py
from django.contrib import admin
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import include, path
from django.views.generic.base import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path(
'favicon.ico',
RedirectView.as_view(url=staticfiles_storage.url('upload.ico'))
),
path('', include('uploader.urls'))
]
My directory structure:
../
├ imguploader/
| ├ __init__.py
| ├ settings.py
| ├ urls.py
| └ wsgi.py
├ static/
| ├ admin/
| └ upload.ico
├ manage.py
|...
You can set STATICFILES_DIRS in settings.py, works for me:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.views.generic.base import RedirectView
urlpatterns = [
path('favicon.ico', RedirectView.as_view(url=settings.STATIC_URL + 'favicon.ico')),
path('admin/', admin.site.urls),
]

How to configure static files in Django app for Heroku?

I deployed a django app to heroku, using "git push heroku master". It works fine but i have problem with static files. I can't configure it. What i have to do to get it started? Can you help me guys?
settings.py
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
base.html
<link rel='stylesheet' href='{% static "css/base.css" %}' />
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from accounts.views import (login_view, register_view, logout_view)
from timetable.views import home
urlpatterns = [
url(r'^timetable/', include("timetable.urls", namespace='timetable')),
url(r'^admin/', admin.site.urls),
url(r'^home/', register_view, name='register'),
url(r'^login/', login_view, name='login'),
url(r'^logout/', logout_view, name='logout'),
url(r'^$', home, name='controler'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Try this
import os
from django.conf import settings
from django.conf.urls import include, patterns, url
from django.contrib import admin
# all other necessary imports
admin.autodiscover()
BASE_DIR = os.path.dirname((__file__))
urlpatterns = [
# all my url() patterns
]
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)

Serve media on localhost (windows)

I have an img file which i copied to the /mediafiles/ folder. How can I display it? It just returns 404 (not found). Or is that not possible in DEBUG mode?
I've also tried using {{ MEDIA_URL }}img1.jpg in the template but to no avail.
FYI, I have no problems with static images or stylesheets (as long as I don't forget to run collectstatic).
Django 1.8.3, localhost on Windows, in a virtualenv.
Any ideas would be greatly appreciated.
myproject\myapp\settings.py:
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS= (
os.path.join(BASE_DIR, 'static/myapp/theme_one'),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/myapp/theme_one'),
)
MEDIA_URL = '/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
myproject\myapp\urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^book/', 'book.views.protopage', name='protopage'),
)
myproject\book\views.py:
def protopage(request):
picurl = settings.MEDIA_URL + 'img1.jpg'
return render_to_response('book/protopage.html', locals(), context_instance=RequestContext(request))
myproject\book\templates\book\protopage.html:
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head><link href="{% static 'css/style.css'%}" rel="stylesheet"></head>
<body>
<img src='{{ picurl }}'/>
</body>
</html>
File structure
MYPROJECT
│ .gitignore
│ manage.py
├───book
│ views.py
├───myapp
│ settings.py
│ urls.py
├───mediafiles
│ img1.jpg
├───static
│ └───myapp
│ └───theme_one
│ ├───css
│ │ style.css
│ └───img
│ logo.png
├───staticfiles
├───templates
│ └───myapp
│ └───theme_one
│ └───book
│ └───protopage.html
└───venv
I got it to work after reading https://docs.djangoproject.com/en/1.8/ref/views/
In my urls.py file I need to add the static.serve() view:
from django.conf import settings
. . .
urlpatterns = patterns('',
url(r'^book/', 'book.views.protopage', name='protopage'),
(r'^mediafiles/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT
}),
)