I've finished a beginner's tutorial on django-project and now I am trying to go further.
So, I want to add an ability to add images to polls. To do that, I need to add 'django.template.context_processors.media' to context_prosessors and create MEDIA_ROOT.
So i did
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(str(BASE_DIR))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL)
print(str(MEDIA_ROOT))
But when I run the django-server I see the following results of the print functions:
What am I doing wrong? And why do I see the results of the print functions twice?
Change:
MEDIA_URL = '/media/'
To:
MEDIA_URL = 'media/'
Study this example to know what is happening:
import os
print(os.path.join('a', 'b/')) # prints "a/b/"
print(os.path.join('a', '/b/')) # prints "/b/"
Related
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
}
An image in media_root does not get displayed.
My code is located at https://github.com/tomaszm-web/Django-ecommerce
Can you please help me to work out why the image does not get displayed?
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I think you are using code of older version but you have django 3. You can try this code.
I replaced
re_path(r'products/(?P<pk>\d+)/', ProductDetailView.as_view()),
re_path(r'products-fbv/(?P<pk>\d+)/', product_detail_view),
with
path("products/<int:pk>/", ProductDetailView.as_view()),
path("products-fbv/<int:pk>/", product_detail_view),
in urls.py file and it works now.
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/')
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__))
I'm using wysiwyg redactor for admin page. So I can add some images to my articles. Firstly, In settings.pyI wrote:
REDACTOR_UPLOAD = '/media/uploads/'
MEDIA_ROOT = '/media/'
MEDIA_URL = '/media/'
in this case, all uploaded images are in C:\media\uploads. It's working.
But I need images to be located in project folder. So I write:
REDACTOR_UPLOAD = os.path.abspath('/media/uploads/')
MEDIA_ROOT = os.path.abspath('media')
MEDIA_URL = '/media/'
Then image location: src="/media/C%3A/virtenvs/web/src/mysite/media/uploads/CAM00415.jpg"
But when debugging settings.py, MEDIA_ROOT = 'C:\\virtenvs\\web\\src\\mysite\\media'
Why?
You put a relative path in MEDIA_ROOT and an absolute path in REDACTOR_UPLOAD. Compare the following two in an interactive console:
>>> os.path.abspath('media/')
C:\\Users\\<username>\\media
>>> os.path.abspath('/media/')
C:\\media
C:\Users\<username> is the current working directory here. Unless you use an absolute path (starting with a /), the path in abspath will get appended to your current directory. The working directory of django seems to be C:\virtenvs\web\src\mysite, which also appears to be your project's directory, so in this case using a relative path for both settings should work:
REDACTOR_UPLOAD = os.path.abspath('media/uploads/')
MEDIA_ROOT = os.path.abspath('media/')
MEDIA_ROOT must be an absolute filesystem path, example:
MEDIA_ROOT = 'C://media/'
to make it inside project folder:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media').replace('\\','/')