Issue
I am trying to upload some images in my app media folder. The problem is images uploaded successfully but not in media folder. Its creates another folder and store their. My code details are given bellow:
urls.py
from django.contrib import admin
from django.urls import path
from ssibdweb import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIAFILES_DIRS)
settings.py
MEDIA_DIR = BASE_DIR / 'media'
MEDIA_URL = '/media/'
MEDIAFILES_DIRS = [
STATIC_DIR,
]
models.py
class BlogImage(models.Model):
id = models.AutoField(primary_key=True)
sub_id = models.ForeignKey(Subject, on_delete=models.CASCADE)
pic = models.ImageField(upload_to='blog/')
def __str__(self):
return str(self.id)
I am using following form for upload
<form method="POST" enctype="multipart/form-data" >
Solution
You're trying to pass settings.MEDIAFILES_DIRS
inside static()
function which is not exists in django.conf
settings you can check available constants of settings. In order to store your uploaded media content you've to set MEDIA_ROOT
value like this
MEDIA_ROOT = BASE_DIR / 'media'
and inside your urls.py file change like this
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Answered By - Ankit Tiwari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.