Django file upload using Form

佐手、 提交于 2019-12-04 06:12:31

问题


Hy there, my first question on this website, so sorry for my english. So i try to upload a file on a model in Django framework.

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)

this is the model

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)

this is the form

def add_form(request): form=BannerForm() last=models.banner.objects.all().last()

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})

this is the view.Now every time i try to add a banner.I browse the file, but after i push submit, it is that the file must be chose, like it don't recongnize what i browse to the form button.Any suggestion?


回答1:


well you need to specify the upload path in your models

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

and make sure you have MEDIA_ROOT and MEDIA_URL defined in your settings.py

in your form

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



回答2:


You need to include enctype="multipart/form-data" in your form definition.

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



回答3:


Try this something like this :

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())

And like already said, don't forget :

<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>

You don't have to specify these things :

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

new_banner.register_date=form.cleaned_data['register_date'] -> useless because in your model you set auto_now_add=True, so you don't need te make a field in your HTML form.

Note : if your field created_by = models.CharField(max_length=255) is to put an existing user, so you should make a foreign key field like :

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


来源:https://stackoverflow.com/questions/45727293/django-file-upload-using-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!