Django 1.10 add media url to static image file - django

After update to Django 1.10 I have a problem with static file images. Now Django add to static file path "media" link... For example before it was "/static/images/avatar.jpeg" now "/media/static/images/avatar.jpeg"
Admin
Settings
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(BASE_DIR, 'static'),
'static',
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

Related

Django static files not loading properly

I tried to get my project set up with static files and I do not seem to be successful. I want a global static files instead of an in-app one and so my settings.py is
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
And as such my directory is
The issue I face is that my vendor folders will not load. Only images do and my custom css file.
What I did is move animate.min.css to the css folder and it worked but I wonder if being inside many folders affects it.
root urls.py
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # It will be deactivated when running collectstatic. Active on localhost
#STATIC_ROOT = os.path.join(BASE_DIR, 'static/') # It will be activated when running collectstatic. Active on server
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Static and media files are not working on django real server

In the result (real server) the css, js and images are not connected, but the thing is that in localhost it works perfect. I dont know what any other details do you need so write comment and I will edit this queston :)
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CRISPY_TEMPLATE_PACK = 'uni_form'
LOGIN_URL = ''
Most probably you misconfigured nginx file or did not mention in nginx file.
You need to write path in nginx file something like below:
location static_url {
alias static_dir
}

django-inlinecss returns error You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path

I am trying to load a page as PDF. actually it loaded successfully - without any style or even the images. So I installed django-inlinecss and added it in installed_apps in settings.py and loaded it in template and called it as I was told. but it returns an error
You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
and points at:
1 {% load inlinecss %}
2 {% load static %}
3 {% inlinecss "css/style.css" %} <---
I don't have any idea how to fix it. Hope someone could help me in this
as the error say you have to specify the path of your static and media files in settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
actually it worked when I set directory for STATIC_ROOT. but there is one thing to be noticed that you have to set another directory for that. Which means STATIC_URL and others like STATIC_FILE_ROOT, STATICFILES_DIRS shouldn't be the same as STATIC_ROOT
eg:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATIC_URL = '/static/'
STATIC_FILE_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "static/assets")
sorry if that's not what you are looking for.

Setting multiple MEDIA_URL & MEDIA_ROOT in django

I've set Static and Media root as well as url's in my django app, as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/crl/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'config/crl/')
It is working great, but I want to add another MEDIA_URL & MEDIA_ROOT to serve files from the /certs/ directory as follows:
NEW_MEDIA_URL = '/certs/'
NEW_MEDIA_ROOT = os.path.join(BASE_DIR, 'config/certs/')
Is there any way to do it?
I'm using Django 2.0.6 and Python 3.5
Multiple static URLs and static roots can be added to Django using the following steps.
Configure a BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Create as many static roots and static URLs as you need
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# the "static/" above is a directory inside the Django Project Directory
STATIC_URL_1 = '/static-1/'
STATIC_ROOT_1 = os.path.join(BASE_DIR, "static_1/")
Similarly, you can create as many media roots and media URLs as you need
MEDIA_URL = '/crl/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'config/crl/')
MEDIA_URL_1 = '/crl-1/'
MEDIA_ROOT_1 = os.path.join(BASE_DIR, 'config/crl_1/')

Media files end up in in a pycharm subdirectory when uploading

I am building a Django application on a Windows system where the user should be able to upload images.
But somehow my uploades files end up in my Program Files directory at:
C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.5.1\jre\jre\bin\WebApp\media\TestData\static\images
I just can't understand why I get this result. I would like to store these Images on another disk such as:
G:\General\Users\Me\WebApp\media
I have been trying to change my MEDIA_ROOT but without any success. Im not even sure if that's were the problem is or if I don't fully understand how uploading with Django works.
These are my settings:
import os
BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__')))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
os.path.join(BASE_DIR, 'staticfiles'),
)
Don't know if it matters but i am using a ModelForm from a model with an Imagefield.
Your BASE_DIR should look like:
os.path.dirname(os.path.dirname(__file__))