I am starting Django. I think my problem is trivial, but I canβt solve it. I have a model called Document with one FileField:
class Document(models.Model): file = models.FileField(upload_to="documents") created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) category = models.ForeignKey(DocumentCategory) title = models.CharField(max_length=255, unique=True) description = models.TextField() def __unicode__(self): return self.title
I want to add a new instance of this class using ModelForm:
class DocumentForm(ModelForm): class Meta: model = Document
In views.py, I have:
def add_document(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/') else: return render_to_response('add_document.html', {'form':form}, context_instance=RequestContext(request)) else: form = DocumentForm() return render_to_response('add_document.html', {'form':form}, context_instance=RequestContext(request))
The template for this (i.e. add_document.html):
{% extends "base.html" %} {{block content %} <form enctype="multipart/form-data" method="post" action="">{% csrf_token %} {{form}} <input type="submit" value="Add document" /> </form> {% endblock %}
In the admin interface, adding the model to the database that works correctly, and the added file is located in the localization "upload_to". My form is not working. When I try to submit a form, I get a filefield form error: "This field is required!" Without FileField in the model, this works earlier. I have Django 1.2.5 I have been tormenting him for 3 days and nothing! I'm desperate. Sorry for my language. Please, help!
source share