Every thing works perfect in debugging mode but can't show the media files on production environment while debugging id false
That's my settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.csrf',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.static',
'django.template.context_processors.media',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.core.context_processors.debug",
"django.core.context_processors.auth",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
)
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')
# Media folder for database media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and the project urls
if settings.DEBUG:
urlpatterns += static(
settings.STATIC_URL,
serve,
document_root=settings.STATIC_ROOT
)
urlpatterns += static(
settings.MEDIA_URL,
serve,
document_root=settings.MEDIA_ROOT
)
Although the url link path shows perfectly using template tags
{% load staticfiles %}
<a href="{{ magazine.document.url }}" target="_blank">
<i class='far fa-file-pdf'></i>
</a>
Hope you can help to fix this issue.
It was my fault PDF files must be downloaded or viewed by third party viewer.. and for debugging mode I must have Apache server to work when debugging is False... Hope this help any one face the same issue
Related
i have problems with my django app. Since a few days i have the problem that the download dont work on production but i cant find the reason why...
I can upload files and they are stored in the correct folder, but as soon as i click on it i get a 404 error "file not found.."
Here is my setup:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
.
.
.
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
MEDIA_DIR = os.path.join(BASE_DIR, "media")
STATIC_ROOT = STATIC_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "staticfiles/static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_DIR
MEDIA_TEMPLATE_ROOT = os.path.join(MEDIA_ROOT, 'templates')
MEDIA_TEMP = os.path.join(MEDIA_ROOT, 'tmp')
urls.py
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('jet/', include('jet.urls', 'jet')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I tried many different configurations of the media_url, media_root etc... without success..
In production, usually the django web app is behind a web server like nginx or apache http web server.
In that case, what is usually done is to configure your web server (nginx or apache) to serve directly the static files, thus you need to configure the virtualhost section.
If you can provide more information about your configuration, I could be more specific.
Thats the setup iam using:
https://lab.uberspace.de/guide_django/
Its my first time deploying the app on a server. Debug=False yes. The app it self works but it doesnt serve the files. But i have no clue what gunicorn does in this configuration, i just followed the link above to set it up.
The solution was to configure the uwsgi file:
static-map = /static=%(chdir)/staticfiles/static
There I modified the location of the media folder to the staticfiles/static folder. Then it worked!
I removed the BASE_DIR and the os import because I am using django-environ.
However when I entered the admin dashboard of Django, the css is not read so I only see plain html elements.
Here is how part of my settings.py looks like.
import environ
root = environ.Path(__file__) - 3 # get root of the project
env = environ.Env()
environ.Env.read_env() # reading .env file
SITE_ROOT = root()
DEBUG = env.bool('DEBUG', default=False)
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = tuple(env.str('ALLOWED_HOSTS', default=[]))
DATABASES = {
'default': env.db('DATABASE_URL')
}
SECRET_KEY = env.str('SECRET_KEY')
# Static and Media Config
public_root = root.path('public/')
MEDIA_ROOT = public_root('media')
# MEDIA_ROOT = root.path('media/')
MEDIA_URL = env.str('MEDIA_URL', default='media/')
STATIC_ROOT = public_root('static')
STATIC_URL = env.str('STATIC_URL', default='static/')
ROOT_URLCONF = 'myproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [root.path('templates/'), ],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
So I also put in root.path('templates') into the DIRS of TEMPLATES.
I also added this snippet below to the project's urls.py.
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
I also have this on my .env file:
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
MEDIA_URL=media/
STATIC_URL=static/
Where could I have gone wrong and what should be written instead?
I looked into djangogirls tutorial and found out that I missed out some slashes.
From my .env file:
MEDIA_URL=media/
STATIC_URL=static/
I changed it into
MEDIA_URL=/media/
STATIC_URL=/static/
Template cant find static files and load it on page.
This is my settings:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'cargo/templates')
# STATIC_DIR = os.path.join(BASE_DIR,'cargo/static')
DEBUG = True
ALLOWED_HOSTS = []
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = 'os.path.join(os.path.expanduser("~"), "domains/h2h.su/static/")'
This my path to files cargo/static/cargo/images and templates at the same path in cargo app cargo/templates/cargo/index.html
Can someone help me?
I've fixed the problem by changing os.path.join(BASE_DIR, "static"),
to os.path.join(BASE_DIR, "cargo"),
I'm using Vue + webpack with a Django backend for my webapp and I need to be able to serve files from the dist folder from Django when the user hits the root url '/'. The dist folder containing the static files from Vue + webpack has already been created, and I just need to know how to serve those files from Django. I disabled collectstatic on Heroku because all my files will be served from dist folder that was not generated by django.
I'm able to serve index.html from the dist folder but not the js and css files in there. The error i'm getting is /static/js/main.js not found
Here's what I have so far, but none of my attempts are working. I need some way to configure Django urlpatterns to serve the static files in the dist folder
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '../dist')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.static',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_ROOT = os.path.join(BASE_DIR, '../dist')
STATIC_URL = '/dist/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../dist'),
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
urls.py
if settings.DEBUG is False:
urlpatterns += url(
urlpatterns += url(r'^$', generic.TemplateView.as_view(template_name='index.html')),
dist/
- index.html
- static
- js
- main.js
- css
- main.css
I am little bit confused about django templates, I have attached screen shot of workspace, can some one please tell how can I use addprofile.html template in my views.py in buddy app.
In my settings/base.py I have mentioned templates in following way:
# Build paths inside the project like this: join(BASE_DIR, "directory")
BASE_DIR = dirname(dirname(dirname(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATICFILES_DIRS = [join(BASE_DIR, 'static')]
MEDIA_ROOT = join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
# Use Django templates using the new Django 1.8 TEMPLATES settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
join(BASE_DIR, 'templates'),
# insert more TEMPLATE_DIRS here
join(PROJECT_PATH, 'templates/')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I am new to Django and python and this is my first app in django
create following structure:
buddy (this is your existing budy app folder)/templates (this is existing folder)/buddy (this is new folder) and place all your templates: addprofile.html, edit_profile.html, show_profile.html, success.html withing this path.
edit your settings 'DIRS' so it looks as follows:
'DIRS': [
join(BASE_DIR, 'templates'),
join(PROJECT_PATH, 'templates/'),
join(BASE_DIR, 'buddy/templates')