我对django很陌生。当我试图写一个处理上传文件的页面时,我遇到了一个问题。这是代码:
class HomeworkContent(models.Model):
homework = models.ForeignKey(Homework, related_name="content_homework")
student = models.ForeignKey(User, related_name="content_student")
grade = models.IntegerField(blank = True, default = -1)
description = models.CharField(blank = True, max_length = 1000)
file = models.FileField(upload_to='file/%Y/%m/%d', blank=True,null=True)
@login_required
def submit_homework(request, course_id, assignment_id):
user = request.user
form = HomeworkContentForm(request.POST, request.FILES)
if request.method == 'POST':
if form.is_valid():
content = HomeworkContent()
content.file = request.FILES['file']
content.description = form.cleaned_data["description"]
content.save()
return render_to_response('assignment_detail.html', locals(), RequestContext(request))
else:
form = HomeworkContentForm()
return render_to_response('assignment_submit.html',locals(), RequestContext(request))
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
<table>
<tr><th><label for="id_description">description:</label></th><td><input id="id_description" maxlength="1000" name="description" type="text" /></td></tr>
<tr><th><label for="id_file">File:</label></th><td><input id="id_file" name="file" type="file" /></td></tr>
</table>
<input id="submit" type="submit" value="submit" />
</form>
class HomeworkContentForm(forms.Form):
description = forms.CharField(label = u'description', required = False, max_length = 1000)
file = forms.FileField()
那么问题是什么?如果有人可以帮助我,我将非常感激。