Django文件上传失败

时间:2013-05-20 12:35:31

标签: python django django-forms

我对django很陌生。当我试图写一个处理上传文件的页面时,我遇到了一个问题。这是代码:

models.py:

   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)

view.py:

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

HTML:

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

forms.py

class HomeworkContentForm(forms.Form):
    description = forms.CharField(label = u'description', required = False, max_length = 1000)
    file = forms.FileField()

那么问题是什么?如果有人可以帮助我,我将非常感激。

0 个答案:

没有答案