How to serve static media with Daphne 2.0 on django - django

I'm new on daphne and I would like to know how to deploy a django app running on daphne, on ubuntu server. I already configured the app like the documentation said, and works fine, except that the static-files (js,css,imgs,etc) doesn't load. What I need to do?

Sorry, was a mistake. I recently noticed that when I was using Channels 1.8, I had this code on routing.py
on production
from channels.staticfiles import StaticFilesConsumer
from . import consumers
channel_routing = {
# This makes Django serve *emphasized text*static files from settings.STATIC_URL, similar
# to django.views.static.serve. This isn't ideal (not exactly production
# quality) but it works for a minimal example.
'http.request': StaticFilesConsumer(),
# Wire up websocket channels to our consumers:
'websocket.connect': consumers.ws_connect,
'websocket.receive': consumers.ws_receive,
'websocket.disconnect': consumers.ws_disconnect,
}
Its likely that was the reason. that on 1.8 was working and 2.0 don't.
Besides
Andrew Godwin (Mantainer of daphne and channels) commented me
"Daphne will only serve static files from the same process in local dev via runserver - once you deploy to production, you need to run collectstatic and serve static files separately as you can read here: https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-static-files-in-production"

Use these settings, they have worked fine for me. We have two separate folders here. One for media files and other for static files.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn",
"static_root")
MEDIA_URL = '/media/'
MEDIA_ROOT =
os.path.join(os.path.dirname(BASE_DIR),"static_cdn","media_root")

Related

Django app on Azure incorrectly loading static files from an Azure Blob

This Django app is deployed on Azure as an App Service its static and media files are stored in an Azure storage account - blob.
The project used to work well in the past, but something has changed and now the problem is as following.
Relevant part of the app settings file:
STATIC_URL = 'https://myappstorage.blob.core.windows.net/static/'
MEDIA_URL = 'https://myappstorage.blob.core.windows.net/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# any static paths you want to publish
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
so one would expect that for example a favicon, which is in the root directory of the storage could be found on https://myappstorage.blob.core.windows.net/static/favicon and it is indeed!
But all static files that the app on azure tries to load it tries to load from
https://myappstorage.myappstorage.blob.core.windows.net/static/
(note the duplication of myappstorage), same for media files.
This results in no static files being applied to the page as they are being loaded from the wrong url. When I run the app locally, it works fine. I destroyed it and recreated it, no success. Now I have two copies running, one deployed through FTP and startup command and on using Github action. Still the same problem.
I have also tried little hack-ish workarounds in the setting file, with no success.
Try with adding below in app setting
DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'
STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = "djangoaccountstorage"
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION }/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}
refer this document for more details

having problem with django static file set up

I can only and only set the static files in Django with this code
In Installed Apps in settings.py
'....',
'staticfiles',
'....
In end of settings.py
STATIC_URL = '/staic/'
STATICFILE_DIRS = [
os.path.join(BASE_DIR, '/static')
]
And It worked. At My First Time, I also tried this
In urls.py
#this way
urlpatterns = [
...,
...,
.....,
] + static(STATIC_URL, document_root=STATIC_ROOT)
#or
urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
At the first time it worked but after that project this way didn't work
I viewed thousands of websites.
I did all the thing in the code like the youtube tutorial
But the second code didn't work anymore. But I cannot use the development server in production. But the first code (which works for me) requires a development server.
If Anyone knows please (if possible) give me the example code to try it, the possible answer, and all the websites where I can know more. It would be helpful.
Thanks Very Much
in settings.py write this code
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Is not a metter of URLs in your application.
As you say, django development server is not suitable for production environment.
You need a reverse proxy like Apache or Nginx in order to be able to serve the static files to your application and WSGI HTTP Server to run your application like gunicorn
Here is a link with a very nice guide on how to setup gunicorn and nginx for django production environment.
DigitalOcean
Vexxhost
Medium

django: how to load the static files with hash/md5 appending correctly?

using Django 3
I followed the Django Doc
https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
to export my static files with a hash appending.
settings.py production
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
static_root folder (output)
static_root/
staticfiles.json
static_root/css/
project_styles.87c2920e7bc3.css
project_styles.css
everything is collected correctly.
Afterwards i uploaded everything to my apache static server.
And i set off / comment the STATICFILES_STORAGE . That is how i understand the Doc´s? If i leave this setting on in production i get an 500 Error.
settings.py production
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
After restarting my Django app in production, my site is still loading project_styles.css but not the hash Version project_styles.87c2920e7bc3.css in my browser. Even if i delete project_styles.css Django will not serve the hash version.
Question
Did i miss some settings in the settings.py in production mode?
In the Doc´s they mention to set STATICFILES_STORAGE = django.contrib.staticfiles.storage.StaticFilesStorage but it shows no difference. And as it is mentioned it´s only for testing.
What i have to do to load the correct static hash version in production? do i have to set something in my templates, so that django will look into the json file for the correct hash version? Or do i have to name the hash file?
Alright, the Problem was that i wanted two different STATIC_ROOT Paths. One for Development and one for Production, because i want all my Development stuff in one Project folder. Because if you collectstatic with the STATIC_ROOT of your apache server, django will export it for instance into c:/var/www/your/server/static while i wanted it to c:/webprojects/myproject_1/static_root_exports and later upload these files on my server separately.
So i set two different Path depending on DEV_STATIC off / on in my django-environ file. Django will set the correct Path.
.env
DEBUG=off
# --- applies media server & sets MEDIA_ROOT & STATIC_ROOT
DEV_STATIC=on
<...>
STATIC_ROOT_DEV=static_root_exports
STATIC_ROOT_PROD=/var/www/myUserName/html/myproject_assets/static
<...>
setting.py
# -- Set for Hash
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
# --- STATIC_ROOT
if DEV_STATIC == True:
STATIC_ROOT = SERVER_DIR.joinpath(env('STATIC_ROOT_DEV'))
else:
STATIC_ROOT = env('STATIC_ROOT_PROD')

Django Wagtail Deployment Static File Server

After deployment today, for some reason, the project no longer shows style from items that would have been collected during the collectstatic process. I have full access to the files from any browser. I have checked all permissions. I have browsed to the site from the development server, and other machines to eliminate software\font possibilities. No idea what's going on here.
I serve the files from a different server. Other django projects are unaffected. No other django projects use anything like wagtail though. Pulling my hair out at this point and probably just missing something simple. What am I missing?
base.py config
STATIC_ROOT = '/var/www/html/static.xxxxxx.net'
STATIC_URL = 'http://static.xxxxxx.net/'
MEDIA_ROOT = '/var/www/html/media.xxxxxx.net'
MEDIA_URL = 'http://media.xxxxxx.net/'
Checking for file existence on server:
-rw-rw-r-- 1 xxxxxx xxxxxx 13648 Aug 24 09:18 /var/www/html/static.xxxxxx.net/wagtailadmin/css/userbar.3d5222a7eba2.css
Checking CSS relative references
-rw-rw-r-- 1 xxxxxx xxxxxx 68812 Aug 24 09:18 /var/www/html/static.xxxxxx.net/wagtailadmin/css/../../wagtailadmin/fonts/opensans-regular.45f80416d702.woff2
Django Debug Toolbar shows 4 static files used for both the production and development environments. Everything is identical.
In the chrome inspect view, if I replace a relative CSS stylesheet link in the development environment with a link from the file server, it breaks the same way.
From:
/static/wagtailadmin/css/userbar.css
To:
http://static.xxxxxx.net/wagtailadmin/css/userbar.css
Again, I can stick that address in my browser, any browser, and I see the stylesheet. I really have no idea how my file server could be stopping browsers from processing CSS, but that's what it's starting to look like.
Update: in the chrome inspect view, if I remove a css reference that uses a stylesheet from my fileserver, the page loses all the style\colors\etc. If I reapply the link to my server, it applys the styles again. It seems to apply all the style except icons\glyphs.
Update 2: If I change to STATIC_URL = '/static/' I get the styles... Until I turn debug back off :-/
Wagtail's recommended setup for STATIC_ROOT, STATIC_URL, MEDIA_ROOT, and MEDIA_URL is typically much simpler and the ..._URL declarations are typically not absolute references. I set things up in base.py like this:
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(os.path.join(BASE_DIR, directory_structure_down_to..., 'static'),
--- include as many lines like above as necessary here ---
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
If you use wagtail-react-streamfield, update it. The font wagtail.woff is duplicated there and it gets picked up first (before wagtail's original one) by Django's static subsystem when collecting statics.

Django Admin CSS files are missing

I deployed my django app to pythonanywhere.com
and my admin css not working
http://directdirect.pythonanywhere.com/admin/login/?next=/admin/
what do I do?
I use django 2.0
settings.py:
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, 'django_direct/main_app/static'),
os.path.join(BASE_DIR, 'django_direct/main_app/static'),
http://directdirect.pythonanywhere.com/static/admin/css/dashboard.css
]
Check your settings.py file.
DEBUG = True # debug true mod working admin css
DEBUG = False # debug false mod not working css
You can fix it using a proxy server or by running Django with the insecure parameter.
Use NGINX Proxy Server: django-with-nginx
Use Insecure Parameter: run-django-with-insecure-argument
I assume you followed similar instructions to this page from help.pythonanywhere.com.
There it says:
Go visit your site, it should be live! But it probably won't be using your CSS stylesheets, JavaScript and other things that are loaded from static files. To get those set up, check out the page configuring static files with Django.
And here is the link to get those static files up and running.
This should point you in the right direction.
Just move the admin folder from static_cdn to static folder