Django media folder is not created when uploading image

Viewed 2363

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

models.py

class Profile(models.Model):
    img=models.ImageField(upload_to='media',null=True)
    bio=models.TextField(max_length=50, null=True)
    user=models.OneToOneField(User,related_name='profile',on_delete=models.CASCADE)

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

so basically after I upload an image no media folder is created, i am working on latest version of django, if more info need to be provided, please tell.

EDIT: i created a media folder as someone suggested, but the image is not displaying, and it is not inside the media folder i have created

<html>
    <body>
        <img src="{{ user.profile.img.url }}" alt="{{profile.user}}">
        <br>
        <br>
        {{profile.user}}
    </body>
</html>

this is the code used to display image

the output of html in developer tool console is :

<html>
    <body>
        <img src="" alt="somename">
        <br>
        <br>
        somename
    </body>
</html>

the html code that is responsible for uploading the image:

<html>
    <form method='post' enctype="multipart/form-data">
        {% csrf_token %}
        <input type="file" id="img" name="img" accept="image/*">
        <textarea name='bio' placeholder='Write about yourself'></textarea>
        <input type="submit">
    </form>
</html>

views.py

#this function responsible for uploading image
def user_profile_view(request,pk):
    user=User.objects.first()
    if request.method == 'POST':
        img=request.POST.get('img')
        bio=request.POST.get('bio')
        profile=Profile.objects.create(img=img,bio=bio,user=user)
    return render(request,'profile.html')

#this function responsible for displaying profile.

def user_profile_display(request,pk):
    user=get_object_or_404(User,pk=pk)
    profile=Profile.objects.get(user=user)
    return render(request,'display_profile.html',{'profile':profile})




3 Answers

You should create the media directory yourself.

mkdir media
echo media >> .gitignore  # use this command to make the git ignore all the media files
echo media >> media/README.md
git add -f media/README.md  # use this command to make sure that
                            # when you use the project in other host, 
                            # the media directory will be created automatically

Check if you are properly recieving the image in the views.py,

Use request.files to get the img. please check if the media folder is in your base directory and the subfolder(if any) is named correctly.

views.py :

if request.method == "POST":
    
    answer_form = Answer_form(data=request.POST)

    if(answer_form.is_valid()):
        ans = answer_form.save(commit=False)
        #ans.user = user

                
        if 'img' in request.FILES:
            ans.img = request.FILES['img']
        
        ans.save()
    
    else:
        print(answer_form.errors)

Here is the documentation page :

File Uploads

Assume that your settings at settings, and your images have been uploaded successfully (otherwise your need to check path and folder permisson setting)

MEDIA_DIR = f'{BASE_DIR}/media'
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

Then you should use like this:

<img src="/media/{{ user.profile.img}}" alt="{{profile.user}}">

 
Related