我在django的团队项目上工作。我有文件上传的问题。它是一个Web应用程序,可以创建,共享表单和一些其他选项(图形,图表,....)。我应该提一下,我是django(初学者)的新手
1.models.py :
class Form(TimeStampedModel, TitleSlugDescriptionModel):
author = models.ForeignKey(User,
on_delete=models.CASCADE)
title = models.CharField(max_length=512)
is_final = models.BooleanField(default=False)
is_public = models.BooleanField(default=False)
is_result_public = models.BooleanField(default=False)
image = models.ImageField(upload_to="upload_location", null=True,
blank=True, width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
file = models.FileField(upload_to="upload location", null=True,
blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('form-detail', kwargs={'slug': self.slug})
2。 forms.py:
class DocumentUpload(forms.ModelForm):
class Meta:
model = Form
field = ["image", "file"]
3. 当然,我进行了迁移,更改了主要设置(网址,MEDIA_ROOT等)
4。 views.py这是我的问题 我尝试修改现有的" create_form(request)"功能。 在任何教程中,我们使用" form = form from forms.py",在我的项目中,我们使用" form = model from models.py"。我应该如何修改此功能以完成此上传文件。
def create_form(request):
if request.method == 'POST':
user = request.user
data = ParseRequest(request.POST)
parsed_form = data.form()
parsed_questions = data.questions()
form = Form(request.FILES, author=user,
title=parsed_form['title'],
is_final=parsed_form['is_final'],
is_public=parsed_form['is_public'],
is_result_public=parsed_form['is_result_public'],
description=parsed_form['description'])
form.save()
for d in parsed_questions:
question = Question(request.FILES, form=form, question=d['question'])
question.save()
for opt in d['options']:
option = Option(question=question, option=opt)
option.save()
return render(request, 'forms/form_form.html', {})
我很感激任何帮助,thx
答案 0 :(得分:0)
我需要使用forms.py中的表单并手动添加其他字段。 THX