简体   繁体   English

如何在 django 中上传 word 或 pdf 文件

[英]How can upload word or pdf file in django

I am trying to upload word/pdf file but it's not working.我正在尝试上传 word/pdf 文件,但它不起作用。 My form submit successfully but file not store in document field.我的表单提交成功,但文件未存储在文档字段中。 It show empty field and it also not show any error during submit the form, i don't know where is the issue and how can i fix the issue.它显示空白字段,并且在提交表单期间也没有显示任何错误,我不知道问题出在哪里以及如何解决问题。 I add the MEDIA URL in setting and also define the path in urls我在设置中添加 MEDIA URL 并在 urls 中定义路径

View.py视图.py

 class SaloonRegistration(TemplateView):
    template_name = 'saloonRegistration.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)

    def post(self, request):
       saloon = SaloonRegister(
           saloon_name=self.request.POST.get('saloon_name'),
           owner_name=self.request.POST.get('owner_name'),
           address=self.request.POST.get('address'),
           contact_no=self.request.POST.get('contact_no'),
           document=self.request.POST.get('document')
       )
       saloon.save()
       return redirect('menu')

Model.py Model.py

 class SaloonRegister(models.Model):
     saloon_name = models.CharField(max_length=50)
     owner_name = models.CharField(max_length=30)
     address = models.CharField(max_length=30)
     contact_no = models.BigIntegerField()
     document = models.FileField(upload_to='doc/')
is_active = models.BooleanField(default=False)

Template模板

 {% extends 'home.html' %}

 {% block content %}
     <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
          <label for="saloon_name">Saloon Name
              <input type="text" name="saloon_name" placeholder="Enter Your first name">
          </label>
          <label for="owner_name">Owner Name
              <input type="text" name="owner_name" placeholder="Enter Your last_name">
          </label>
          <label for="address">Address
              <input type="text" name="address" placeholder="Enter email address">
          </label>
          <label for="contact_no">Contact No
              <input type="number" name="contact_no" placeholder="Enter your username">
          </label>
          <label for="document"> upload Doc
              <input type="file" name="document" id="document">
          </label>
          <button type="submit">Submit</button>
    </form>
 {% endblock%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM