Почему upload_to в ImageField не работает вообще, когда я пытаюсь сохранить загруженное изображение от пользователя в представлениях
Я пытаюсь сделать простой почтовый сервис с очень простой архитектурой проекта. На данный момент моя бэкенд часть разработки почти закончена, но я остановился уже на два дня из-за того, что понятия не имею, почему не работает сохранение изображений.
Очевидно, я ожидаю, что пользователь загрузит аватар своего профиля и сохранит его по пути, который я задал в upload_to. Путь к изображению должен выглядеть как profiles/UserName/his_profile_img.jpeg
За последние два дня я потратил более 12 часов, пытаясь решить эту проблему.
- This is my model where is my trouble ImageField:
from django.db import models
from datetime import datetime, timedelta
from django.contrib.auth.models import User
def username(user, file):
string_username = str(user).split()[0]
return f"profiles/{string_username}/{file}"
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="Profile")
##### This is here
avatar = models.ImageField(default="/profiles/profile.png", upload_to=username)
name = models.CharField(max_length=30, blank=True)
surname = models.CharField(max_length=30, blank=True)
email = models.EmailField(max_length=64, blank=True)
bio = models.CharField(max_length=1024, blank=True)
def username(self):
return self.user.username
@property
def joined(self):
joined = self.user.date_joined.strftime("%d %B %Y")
is_today = joined == datetime.today().strftime("%d %B %Y")
if is_today:
return "today"
is_yesterday = joined == datetime.date(datetime.today() - timedelta(days=1)).strftime("%d %B %Y")
if is_yesterday:
return "yesterday"
return f"{joined}"
def __str__(self):
return f"{self.user} profile"
- This is a view function, which doesn't save image:
def my_profile(request):
if not request.user.is_authenticated:
return redirect('login')
user_profile = Profile.objects.get(user=request.user)
sent_messages = Mail.objects.filter(from_user_inf=request.user).count()
received_messages = Mail.objects.filter(to_user_inf=request.user).count()
context = {
"profile": user_profile,
"sent": sent_messages,
"received": received_messages
}
if request.method == 'POST':
user_profile.name = request.POST.get("name")
user_profile.surname = request.POST.get("surname")
user_profile.email = request.POST.get("email")
user_profile.bio = request.POST.get("bio")
avatar = request.FILES.get("avatar")
# I've also tried get image by:
##### request.POST.get("avatar")
##### saving image by "with open" command(this is too weird)
if avatar:
user_profile.avatar = avatar
user_profile.save()
messages.info(request, "Saved successfully !")
return render(request, 'my_profile.html', context=context)
return render(request, 'my_profile.html', context=context)
- And my_profile.html file and a piece of code where i set image uploading:
<div>
<img class="img" src="{{ profile.avatar.url }}" width="150" height="150">
<label for="file" cursor="pointer" class="btn">Change Photo</label>
<input id="file" style="visibility:hidden;" name="avatar" type="file" accept="image/*">
</div>
Да, я сделал правильные настройки и добавил его в urls.
- settings.py ---> ROOT and URL media, static settings :
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
- urls.py:
urlpatterns = [
### There are my urls
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Спасибо за внимание, надеюсь, мы сможем решить эту проблему!
Легко с ModelForms
:
# forms.py
from django import forms
# import your model here
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('name', 'surname', 'email', 'bio', 'avatar')
# views.py
# imports...
def my_profile(request):
if not request.user.is_authenticated:
return redirect('login')
user_profile = Profile.objects.get(user=request.user)
sent_messages = Mail.objects.filter(from_user_inf=request.user).count()
received_messages = Mail.objects.filter(to_user_inf=request.user).count()
context = {
"profile": user_profile,
"sent": sent_messages,
"received": received_messages
}
if request.method == 'POST':
form = ProfileForm(
instance=user_profile, data=request.POST, files=request.FILES
)
if form.is_valid():
form.save()
messages.info(request, "Saved successfully !")
# Best use a redirect to a success page here.
# Otherwise, if the user clicks reload, the form will be resent.
return render(request, 'my_profile.html', context=context)
else:
form = ProfileForm(instance=user_profile)
# Use the form in the template! @see: https://docs.djangoproject.com/en/3.2/topics/forms/#the-template
# If you need personalize for bootstrap, etc @see: https://github.com/django-crispy-forms/django-crispy-forms
context['form'] = form
return render(request, 'my_profile.html', context=context)
Если вам нужно загрузить файл без ModelForm
, вручную сохраните файл по пути сервера и затем задайте этот путь (относительно MEDIA_ROOT) в модели. Смотрите handle_uploaded_file
в этой ссылке https://docs.djangoproject.com/en/3.2/topics/http/file-uploads/#basic-file-uploads.