0

If I enter the image manually in the admin panel, it is correct, but if it is entered through the form, it is not saved in the media directory.

views.py


def profile_additional_info(request):
   if request.method == "POST":
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        phone_number = request.POST['phone_number']
        photo_prof = request.FILES['photo_prof']
        kod_meli = request.POST['kod_meli']
        shomare_kart = request.POST['shomare_kart']
   ...
     
   if photo_prof:
       Profile.objects.all().filter(user=request.user).update(
                                         photo_prof=photo_prof)
   ...            
   
   else:
        prof_user = Profile(user=request.user, photo_prof=photo_prof, 
                                phone_number=phone_number,
                                    kod_meli=kod_meli, 
                                shomare_kart=shomare_kart)
        prof_user.save()

models.py

class Profile(models.Model):
    ...
    photo_prof=models.ImageField(max_length=10000,upload_to='photo/%y/%m/%d/',blank=True)
    ...

template:

<form method="post" action="{% url 'profile_additional_info' %}" enctype="multipart/form-data">
...
    <input name="photo_prof" type="file" class="custom-file-input"id="inputGroupFile04"
 aria-describedby="inputGroupFileAddon04">
...
</form>

settings:

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

urls.py:

from django.conf.urls.static import static
from django.conf import settings

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

I found some similar questions on SO but none of them solved my problem.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
MK81
  • 24
  • 5
  • `I put MEDIAROOT, MEDIAURL in settings` please show these settings. And show some more code from the view. If form class is involved - please show this class definition. – Ivan Starostin Apr 27 '23 at 16:13
  • I sent the things you requested and the form class is not involved with the code – MK81 Apr 27 '23 at 18:01

1 Answers1

0

update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save())

Files are not stored in DB, only file paths are stored there. update can only update rows in DB, not files. Thus (actually I never tried to do like this) given code

Profile.objects.all().filter(user=request.user).update(photo_prof=photo_prof)

cannot modify files on drive and just updates the path in DB which will be pointing to nowhere.

Basic file updloads examples in the Django docs don't suggest to run updates, they suggest to instantiate the model and save it. In your case I guess the Profile already exists and you need to update the picture. Try this code (after if):

profile = get_object_or_404(Profile, user=request.user)
profile.photo_prof = photo_prof
profile.save()

You already did some research on SO - here are some more posts you might want to look into. As said in the last one it is recommended to start using forms. They do lots of work for you.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39