Model:
content = RichTextUploadingField(blank=True, null=True)
image = models.ImageField(upload_to="news/%Y/%m")
I set this in settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH = 'news/%Y/%m'
But the images still go to different folders.Is there anyway to make two paths one?
Best regards!
Related
I've created a Blogging App using Django and Heroku-Postgres as Database. The user uploaded images gets removed in Heroku-Postgres after redeployment I think and it just disappears from the website.
I tried fixing this using AWS S3 Bucket but I'm not able to configure the settings in my Django App, the user uploaded files are uploaded to media/images/ according to my models.py
models.py
class Post(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
article_image = models.ImageField(null=True, blank=True, upload_to="images/")
settings.py
AWS_STORAGE_BUCKET_NAME = '#####'
AWS_S3_REGION_NAME = 'us-east-1'
AWS_ACCESS_KEY_ID = '####'
AWS_SECRET_ACCESS_KEY = '####'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
MEDIAFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
DISABLE_COLLECTSTATIC=1
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Now, what am I supposed to remove/keep from these so that the user uploaded images which are now stored in media/images/ are stored in AWS S3 Bucket?
I'm new to programming, I'm trying to put up a mp3 file and player on a django web page the player is showing but, the song is not playing.
**models.py**
from django.db import models
from django.contrib.auth.models import User
class Song(models.Model):
name = models.CharField(max_length=125)
audio_file = models.FileField('media/')
**audio.html**
<!DOCTYPE html>
<html>
<body>
<audio controls>
<source src="JesusChrist.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>
**media folder**
JesusChrist.mp3
**Settings.py**
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'firegod/static/media')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/media/'
This video is a great reference, and should spell everything out. He shows where files go when they are uploaded and how to access them in the templates.
Another problem you have is this:
# Sets STATIC_ROOT
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Sets STATIC_URL
STATIC_URL = '/static/'
# Overwrites STATIC_ROOT
STATIC_ROOT = os.path.join(BASE_DIR, 'media')
# Overwrites STATIC_URL
STATIC_URL = '/media/'
I believe what you want is:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
and then you should be able to find your file at
src="media/audio.mp3". If you need to, print the url of the uploaded file so you know where to find it.
I am about to dive into this, so I might be a little off, though I know the video linked above was very beneficial.
I have a django-admin panel and a Windows Virtual Private Server.
What i want to do is upload files from django admin panel to a directory like this :
C:\site\media
and i dont want to upload files to django app folder .
This is my settings.py file :
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
if not os.path.exists(MEDIA_ROOT):
os.makedirs(MEDIA_ROOT)
and This is my model :
class Pictures(models.Model):
product = models.ForeignKey('Products', models.DO_NOTHING)
picurl = models.ImageField()
def __unicode__(self):
return self.title
class Meta:
managed = False
db_table = 'pictures'
verbose_name_plural = "ProductPicturess"
def __str__(self):
return '%s------- (%s)' % (self.product.title,self.picurl)
How should i change it's values ?
Thank you .
After Searching alot and triying different values finally i found the solutin .
Simply I changed MEDIA_ROOT to this :
MEDIA_ROOT = os.path.join(BASE_DIR, '../../../realwamp/wamp64/www/myproject/pictures/')
And volla , My Problem is solved .
With three pairs of dots like ../../../ I back to the C:\ directory and then go to my www folder .
I want to upload images in the django admin interface. During development everything works fine but when I put the files on my server it doesn't work.
I have two different paths on my Server. One where I put all my source files and one where I put all the static files.
Path for source files: /htdocs/files/project/
Path for static files: /htdocs/html/project/
If I upload an image, then it is saved in /htdocs/files/project/media/. But I want to save it in /htdocs/html/project/. How can I change the path?
Here are my settings:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/var/www/ssd1257/htdocs/html/'
)
And here is my model:
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to="./news/")
The uploaded files are saved normally in the following path
MEDIA_URL + the path specified in “upload_to” attribute in the model class
So in your case,
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') = “/htdocs/files/project/media”
Django will create the path if it doesn’t exits
But I didn’t get the dot in-frontront of ‘upload_to’ to path(“./news/“)
So if you want to change the path where uploaded files are stored, simply change the MEDIA_ROOT
Note, please provide the absolute full path
I guess it will be
MEDIA_ROOT = '/var/www/ssd1257/htdocs/html/project'
Also, its better to rename the uploaded files before saving to avoid file_name conflicts
def get_news_image_path(instance, filename):
path_first_component = ‘news/‘
ext = filename.split('.')[-1]
timestamp = millis = int(round(time.time() * 1000))
file_name = ‘news_’ + str(instance.id) + str('_logo_image_') + timestamp + str('.') + ext
full_path = path_first_component + file_name
return full_path
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to=get_news_image_path)
Now the uploaded files will be saved in
'/var/www/ssd1257/htdocs/html/project/news’
You are Done
In addition, also set appropriate MEDIA_URL
Ex: MEDIA_URL = “media
So when generated URL for the upload images will be
MEDIA_URL + upload_to path
Also, configure the web server to serve these URLs from appropriate locations
from django.core.files.storage import FileSystemStorage
upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/') #upload root set to your project directory
class News(models.Model):
title = models.CharField(max_length=200, null=False)
date = models.DateField(null=False, default=datetime.now)
text = models.TextField(null=False, blank=True)
image = models.ImageField(upload_to='/', storage=upload_storage)
Self-inflicted by this setting:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Change that to:
MEDIA_ROOT = '/htdocs/html/project/'
I am facing some very basic issue.
I saved some detail in my django model including a profile picture.
When i open django admin then i can saw i link for the image which i have saved.But when i click on that link it show me error.My image is not founding.
How can i resolve the issue.
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "staticfiles"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media')
link showing me in django admin
media/media/ind_kQRDi6b.jpg
when i hit enter to open this this image it hit 127.0.0.1/8000/media/media/ind_kQRDi6b.jpg
And error show Page not found
Make sure that your url is correct. Like , http://127.0.0.1:8000/media/image.jpg