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.