[英]django get filename from uploaded file using formset
我正在嘗試使用表單集獲取上載文件的文件名。
views.py
...
elif request.method == 'POST':
albumform = AlbumForm(request.POST)
photoformset = PhotoFormSet(request.POST, request.FILES)
if albumform.is_valid() and photoformset.is_valid():
album = albumform.save(commit=False)
album.user = request.user
album.save()
for photoform in photoformset:
if photoform.is_valid() and photoform.has_changed():
# here is where I'm lost
forms.py
...
class AlbumForm(forms.ModelForm):
class Meta:
model = Album
fields = ('title', 'description')
PhotoFormSet = modelformset_factory(
Photo,
fields=('photo',),
extra=4
)
photoform['photo']
不會直接給我文件名,而是類似
<input type="file" name="form-0-photo" accept="image/*" id="id_form-0-photo">
沒有列出filename
我試過了
photo = photoform.save(commit=False)
print(vars(photo))
{'_state': <django.db.models.base.ModelState object at 0x000001F6326132E8>, 'id': None, 'album_id': 105, 'name': '', 'photo': <ImageFieldFile: phone.png>, 'photo_width': 600, 'photo_height': 416, 'thumbnail': '', 'status': '1'}
我在這里看到了名字,但是必須有一個更簡單的方法來使用它。
最終起作用的是print(photoform.cleaned_data.get('photo').name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.