使用Form的Django文件上传

时间:2017-08-17 05:35:07

标签: django file upload

那里,我在这个网站上的第一个问题,对我的英语很抱歉。所以我尝试在Django框架中的模型上传文件。

class banner(models.Model):
#id is made by Django
name = models.CharField(max_length=255)
created_by = models.CharField(max_length=255)
company = models.CharField(max_length=255)
register_date = models.DateField(auto_now_add=True)
file = models.FileField(null=True, blank=True)
file_name = models.CharField(max_length=255)

这是模型

class BannerForm(forms.Form):
name=forms.CharField(max_length=255)
created_by=forms.CharField(max_length=255)
company=forms.CharField(max_length=255)
data_type=forms.CharField(max_length=255)
register_date=forms.DateField()
file=forms.FileField()
file_name=forms.CharField(max_length=255)

这是表格

def add_form(request):     形式= BannerForm()     最后= models.banner.objects.all()。最后()

if request.method == "POST":
    form = forms.BannerForm(request.POST, request.FILES or None)
    if form.is_valid():
        form.cleaned_data['created_by']
        new_banner=models.banner()
        new_banner.id=last.id+1
        new_banner.name=form.cleaned_data['name']
        new_banner.register_date=form.cleaned_data['register_date']
        new_banner.company=form.cleaned_data['company']
        new_banner.file=form.cleaned_data['file']
        new_banner.file_name=new_banner.file.name
        new_banner.created_by=form.cleaned_data['created_by']
        new_banner.save()

return render(request, "add_banner.html",{"form":form})

这是视图。现在我每次尝试添加横幅。我浏览文件,但是在我按下提交后,必须选择该文件,就像它不会重新识别我浏览到的那样表格按钮。任何建议?

3 个答案:

答案 0 :(得分:2)

您需要在模型中指定上传路径

file = models.FileField(null=True, blank=True,upload_to='files')

并确保您在settings.py

中定义了MEDIA_ROOTMEDIA_URL

在您的表单中

<form method="post" action="" enctype="multipart/form-data">
    {% csrf_token %}
    ...
</form>

答案 1 :(得分:1)

您需要在表单定义中包含enctype="multipart/form-data"

<form method="post" action="your action" enctype="multipart/form-data">
    {% csrf_token %}
    ...
</form>

答案 2 :(得分:0)

尝试这样的事情:

Models.py:

class banner(models.Model):
#id is made by Django
name = models.CharField(max_length=255)
created_by = models.CharField(max_length=255)
company = models.CharField(max_length=255)
register_date = models.DateField(auto_now_add=True)
file = models.FileField(upload_to='files/', null=True, blank=True)
file_name = models.CharField(max_length=255)

forms.py:

class BannerForm(forms.ModelForm):
    class Meta:
        model = banner #Or Banner ??
        fields = ('name', 'created_by', 'company', 'file', 'file_name' )

views.py:

from myapp.forms import BannerForm

if request.method == "POST":
    form = BannerForm(request.POST, request.FILES)
    if form.is_valid():
        entry = form.save(commit=False)
        entry.name = request.POST['name']
        entry.created_by = request.POST['created_by']
        entry.company = request.POST['company']
        entry.file_name = request.POST['file_name']
        form.save()

    else:
      form = BannerForm()

return render(request, "add_banner.html",locals())

就像已经说过的那样,不要忘记:

<form method="POST" action="" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.name}}
    {{form.created_by}}
    {{form.company}}
    {{form.file_name}}
    {{form.file}}
    <input type="submit">
</form>

您不必指定这些内容:

new_banner.file=form.cleaned_data['file']

new_banner.register_date=form.cleaned_data['register_date'] - &gt;没用,因为在你的模型中你设置auto_now_add = True,所以你不需要在HTML表单中创建一个字段。

注意:如果您的字段created_by = models.CharField(max_length=255)是要放置现有用户,那么您应该创建一个外键字段,如:

from django.contrib.auth.models import User
created_by = models.ForeignKey(User, verbose_name="Created by")