UpdateView不在任何地方保存文件或图像,这可能是什么问题?
设置:
MEDIA_ROOT=""
article_form.html:
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
views.py:
from django.views.generic.edit import UpdateView
class updatingexample(UpdateView):
model = Article
fields = ['file1','file2','photo']
url.py
app_name = 'polls'
urlpatterns=[
path('<int:pk>/upd/', updatingexample.as_view(),name="ugh")
]
型号:
class Article(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateField()
file1 = models.FileField(upload_to='uploads/',blank=True)
file2 = models.FileField(upload_to='uploads/%Y/%m/%d/',blank=True)
photo = models.ImageField(upload_to='uploads',blank=True)
def get_absolute_url(self):
return reverse('polls:ugh', kwargs={'pk': self.pk})#back to itself
答案 0 :(得分:1)
只需添加您的表格
enctype="multipart/form-data"
所以它将结束:
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
我还建议您在settings.py中设置MEDIA_ROOT和MEDIA_URL
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
并将其添加到urls.py的末尾
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)