如何在我的 Django 表单中保存或存储用户上传的文档?

伊莎·贾加迪

我有一个 Django 表单,可以接受用户的投诉并将其保存在管理面板中。一切似乎都运行良好。该表单接受数据并将它们全部存储在管理面板中,但上传文件数据除外。该表单确实接受了来自用户的文件并似乎提交了它,但是当其他数据被完美存储时,该文件没有被存储。我如何上传和保存文件?

模型.py:

class Complaint(models.Model):
   user = models.ForeignKey(User, on_delete= models.CASCADE, null = True, blank=True)
   reportnumber = models.CharField(max_length=500 ,null = True, blank= False)
   eventdate = models.DateField(null=True, blank=False)
   event_type = models.CharField(max_length=300, null=True, blank=True)
   device_problem = models.CharField(max_length=300, null=True, blank=True)
   manufacturer = models.CharField(max_length=300, null=True, blank=True)
   product_code = models.CharField(max_length=300, null=True, blank=True)
   brand_name = models.CharField(max_length = 300, null=True, blank=True)
   exemption = models.CharField(max_length=300, null=True, blank=True)
   patient_problem = models.CharField(max_length=500, null=True, blank=True)
   event_text = models.TextField(null=True, blank= True)
   document = models.FileField(upload_to='static/documents', blank=True, null=True)

   def __str__(self):
       return self.reportnumber

视图.py:

def NewComplaint(request):
   user = request.user
   if request.method == 'POST':
       form = ComplaintForm(request.POST)
       print(form)
       if form.is_valid():
           print("hello")
           cmp_obj = form.save(commit=False)
           cmp_obj.user = user
           cmp_obj.save()
       submitbutton= request.POST.get('submit')

       if submitbutton:
           messages.success(request, 'Submitted!')
       context = {'form': form}
       return render(request, 'new.html', context)
   else:
       form = ComplaintForm()
       context = {'form': form}
       return render(request,'new.html',context)

表格.py:

class DateInput(forms.DateInput):
   input_type = 'date'

class ComplaintForm(ModelForm):
   class Meta:
       model = Complaint
       fields = '__all__'
       widgets = {
           'reportnumber': forms.TextInput(attrs={'placeholder': 'Report number'}),
           'event_type': forms.TextInput(attrs={'placeholder': 'Event type'}),
           'eventdate': DateInput(),
           'device_problem': forms.TextInput(attrs={'placeholder': 'Device Problem'}),
           'event_text': forms.Textarea(attrs={'style': 'height: 130px;width:760px'}),
           'manufacturer': forms.TextInput(attrs={'placeholder': 'Enter Manufacturer Name'}),
           'product_code': forms.TextInput(attrs={'placeholder': 'Enter Product Code'}),
           'brand_name': forms.TextInput(attrs={'placeholder': 'Enter Brand Name'}),
           'exemption': forms.TextInput(attrs={'placeholder': 'Enter Exemption'}),
           'patient_problem': forms.TextInput(attrs={'placeholder': 'Enter Patient Problem'}),
       }
    
   def clean(self):
       cleaned_data = super(ComplaintForm, self).clean()
       reportnumber = cleaned_data.get('reportnumber')
       event_text = cleaned_data.get('event_text')
       if not reportnumber and not event_text:
           raise forms.ValidationError('You have to write something!')
       return cleaned_data

模板:

<form class="" action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <p class="sub-typ-wr">Submit Type</p>
            <a href="/Login/Add-Complaint/Document-Style/"><button type="button" class="btn btn-secondary document-btn">Document</button></a>

            <div class="rep-num">
                <label class="written-label" for="">Report Number</label>
                <div class="written-txt-field">{{form.reportnumber}}</div>
            </div>

            <div class="eve-dte">
                <label class="written-label" for="">Event Date</label>
                <div class="written-txt-field">{{form.eventdate}}</div>
            </div>

            <div class="eve-typ">
                <label class="written-label" for="">Event Type</label>
                <div class="written-txt-field">{{form.event_type}}</div>
            </div>

            <div class="dev-pro">
                <label class="written-label" for="">Device Problem</label>
                <div class="written-txt-field">{{form.device_problem}}</div>
            </div>

            <label class="written-label eve-txt" for="">Event Text</label>

            <div class="Manufacturer">
                <label class="written-label" for="">Manufacturer</label>
                <div class="written-txt-field">{{form.manufacturer}}</div>
            </div>

            <div class="pro-code">
                <label class="written-label" for="">Product Code</label>
                <div class="written-txt-field">{{form.product_code}}</div>
            </div>

            <div class="brand-name">
                <label class="written-label" for="">Brand Name</label>
                <div class="written-txt-field">{{form.brand_name}}</div>
            </div>

            <div class="exem">
                <label class="written-label" for="">Exemption</label>
                <div class="written-txt-field">{{form.exemption}}</div>
            </div>

            <div class="pat-pro">
                <label class="written-label" for="">Patient Problem</label>
                <div class="written-txt-field">{{form.patient_problem}}</div>
            </div>



            <div class="comp-textarea">{{form.event_text}}</div>
            <button type="button" class="btn btn-secondary attach-btn-1"><div class="fas fa-file-upload">{{form.document}}</div></button>
            <button type="submit" name="submit" class="btn btn-secondary save-btn-1"><i class="fas fa-save"></i> Save</button>
        </form>

如何从文档文件字段中保存表单?我已经给出了文件应该上传的路径,但文件也没有上传到那里

Mallu开发商

如果你需要太多子目录,你应该提到路径。设置媒体 URL。确保它工作正常
然后request.FILES作为参数传递

form = ComplaintForm(request.POST, request.FILES)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在django中保存注册用户

如何在 django 的扩展用户创建表单中保存自定义添加的字段

我如何在表单上传按钮 django 上使用 javascript

如何在 Django 中保存没有提交按钮的表单

如何在Django模型表单中保存外键

Django如何在Django表单中自动保存用户?

我如何在Django中保存一些数据而不将其添加到表单中?

如何在Django中保存HTML文件?

如何在Django中保存Slug Automatic?

如何在Django中保存模型记录

如何在 Django 中初始化我的表单?

如何在我的 Django 表单中实现下拉列表?

如何在我的django表单中添加引导样式?

在 Django 中保存表单

关于表单的问题,我如何在表单字段 pyhton django 中获得即时用户

我如何在 django 中获取与特定用户相关的 id

如何在 Django 中保存范围滑块(多输入表单)的值

如何在 Slack 中保存用户上传的文件

定义用户在Django中保存上传文件的文件路径

如何在我的 Django 表单中检查和传递用户的实例?

我们如何在Django中通过基于类的通用视图保存表单

如何在Django模型中保存两个不同的用户?

我如何在字典中保存/保存用户输入?

如何在AWS Elastic Beanstalk中托管的Django应用的S3存储桶中保存Django日志文件

如何在Django模型数据库中使用已发布的用户保存表单

如何在一个Django表单工具WizardView中保存具有多个modelForms的多个模型

如何在 Django 模型中保存()一个参数

Django:如何在modelForm中保存预定义值

Django - 如何在查询集对象中保存不同的值?