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('\\','/')
Related
This urls.py of base project
urlpatterns = urlpatterns + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This is the settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT=os.path.join(BASE_DIR,'assets')
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
This code is to add items to the database dynamically and i am not able to understand why is he adding the urlpatterns.
He is adding media urls to the url patterns. So for example if you had an image or video or something stored in your django project, you can use the browser to access these files at MEDIA_URL. In the settings.py, you are setting MEDIA_URL (where you can go in the browser '/media/'), to point to the contents of your MEDIA_ROOT (the 'media' folder)
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 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/"
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/'
I am trying to upload an image to the server. No matter what I do, it doesn't appear to properly upload the image. Here is my field in the model:
image = models.ImageField(upload_to='uploads/images/staff', verbose_name='Staff Member Photo', help_text='Required Dimensions: Square, about 275px height/width')
Here are my settings in the settings.py file:
STATIC_URL = "/static/"
STATIC_ROOT = 'static'
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = 'static/media/'
I can't figure out what's going on--it's driving me crazy!!
You should use the absolute path in the MEDIA_ROOT property. For example:
MEDIA_ROOT = '/var/www/mysite/static/media/'
BTW, what error you get then uploading the image? Is it a path-related problem?