models.py
class Album(models.Model):
file_upload = models.FileField(upload_to=content_file_name)
name_content = models.CharField(max_length=100)
功能就在这里
def content_file_name(instance, filename):
upload_dir = os.path.join('uploads', 'resource')
return os.path.join(upload_dir, filename)
forms.py
class Albumform(forms.Form):
name_content = forms.CharField(max_length=100)
file_upload = forms.FileField( 'please select' )
这是我的views.py
from django.forms.formsets import formset_factory
def viewalbum(request):
AlbumFormSet = formset_factory(Albumform, extra=3, max_num=10,)
if request.method == 'POST':
formset = AlbumFormSet(request.POST, request.FILES)
if formset.is_valid():
albumvalue = Album()
albumvalue.name_content = request.POST.get('name_content', None)
albumvalue.file_upload = request.FILES['file_upload']
albumvalue.save()
return HttpResponseRedirect(reverse('views.viewalbum'))
else:
formset = AlbumFormSet()
return render_to_response('audio/test.html',{'formset': formset}, context_instance=RequestContext(request))
,html看起来像这样
<form action="" method="POST" enctype="multipart/form-data">
{{ formset.management_form }}
{% csrf_token %}
<table>
{% for form in formset.forms %}
{{ form }}
{% endfor %}
</table>
问题是我当时只能上传一个文件。给我解决方案,同时上传多个文件并保存到资源文件夹中。感谢
答案 0 :(得分:0)
如果要上传多个文件,则需要多个文件字段。这是您的选择A.如果您选择使用它,请创建formset并使用它来提供多个文件上传。
您的选项B是使用某种可以上传多个文件的javascript小部件。