Re: request.FILES empty, I think I am doing everything correctly
Hi John
{% csrf_token %} must be definitely inside the <form> tag. It renders <input type="hidden" ...>.
Please try it and tell if it works.
Cheers,
Tom
Dne Thu, 4 Apr 2013 14:38:58 -0700 (PDT)
John <jspayne2@verizon.net> napsal(a):
> This is Django 1.5
> Python 2.7.3
> MYSQL Database
>
> Here is the model that I am using:
>
> class SupportingDocument(models.Model):
>
> """Defines an archived attachment."""
>
> provision = models.ForeignKey(Provision, null = False, blank = False)
>
> submitted_by = models.CharField(max_length = 64, null = False, blank =
> True, default = "")
>
> source_file_name = models.CharField(max_length = 256, null = False,
> blank = False)
>
> content_type = models.CharField(max_length = 64, null = True, blank =
> True, default = "")
>
> submission_time = models.DateTimeField()
>
> version = models.CharField(max_length = 32, null = True, blank = True,
> default = "")
>
> comment = models.TextField(null = True, blank = True, default = "")
>
> saved_file = models.FileField(upload_to = "supporting_documents", null
> = True, blank = True)
>
>
>
> def __init__(self, *args, **kwargs):
>
> super(SupportingDocument, self).__init__(*args, **kwargs)
>
> self.submission_time = datetime.today()
>
>
>
> def __unicode__(self):
>
> return self.source_file_name + ':' + unicode(self.submission_time)
>
>
> and the form definition
>
> class SupportingDocumentForm(ModelForm):
>
> def __init__(self, *args, **kwargs):
>
> try:
>
> provision = None
>
> if 'provision' in kwargs:
>
> self.provision = kwargs.pop('provision')
>
> if 'user' in kwargs:
>
> self.submitted_by = kwargs.pop('user')
>
> except Exception, e:
>
> pass
>
> return super(SupportingDocumentForm, self).__init__(*args, **kwargs)
>
>
>
> class Meta:
>
> model = SupportingDocument
>
> exclude = ('provision', 'submitted_by', 'source_file_name',
> 'content_type', 'submission_time')
>
>
> def save(self, request=None, commit=True):
>
> try:
>
> supporting_document = super(SupportingDocumentForm,
> self).save(commit=False)
>
> supporting_document.source_file = self.source_file
>
> supporting_document.provision = self.provision
>
> supporting_document.submitted_by = self.submitted_by
>
> supporting_document.save()
>
> except Exception, e:
>
> provisionLogger.error('Failure:%s when supplementing group
> details' %(repr(e)))
>
>
>
> def as_table(self):
>
> try:
>
> response = super(SupportingDocumentForm, self).as_table()
>
> except Exception, e:
>
> pass
>
> return response
>
> and the code to handle the request
>
> def supporting_document_new(request):
>
> current_tab = "Data"
>
> current_section = "SUPDOCS"
>
> action_url = urlresolvers.reverse('provision-supporting-document-new')
>
> if request.POST:
>
> try:
>
> form = SupportingDocumentForm(request.POST,
>
> request.FILES,
>
> provision=request.provision,
>
> user=request.user)
>
> if form.is_valid():
>
> supporting_document = form.save()
>
> return redirect_to_url("provision-supporting-document-list")
>
> except Exception, e:
>
> modal_error_message = "Fatal error:" + repr(e)
>
> provisionLogger.error("Error saving Supporting Document %s" %
> (supporting_document.source_file))
>
> else:
>
> try:
>
> form = SupportingDocumentForm(provision=request.provision,
> user=request.user)
>
> except Exception, e:
>
> pass
>
> return render_to_response("provision/suppdocform.html", locals(),
> context_instance=RequestContext(request))
>
> and the html for the form
>
> <p class="modal_form_header">Supporting Document <br/>{{ sourcefile }}</p>
>
> <form method="post" enctype="multipart/form-data" action="{{ action_url }}"
> id="supporting_document_form" class="provision_modal_form" name=
> "supporting_document_form">
>
> <table class="provision_django_form_table">
>
> {{ form.as_table }}
>
> <tr></tr>
>
> <tr style="width: 100%;">
>
> <td colspan=2 style="width: 100%; text-align: center;">
>
> <input type="submit" name="submit" value="Submit" id="submit" />
>
> <span id="end_of_submit_block"></span>
>
> </td>
>
> </tr>
>
> <tr style="width: 100%;">
>
> <td colspan=2 style="width: 100%; text-align: center;" class="errors">
>
> {% if modal_error_message %}
>
> {{ modal_error_message }}
>
> {% else %}
>
>
>
> {% endif %}
>
> </td>
>
> </tr>
>
> </table>
>
> </form>
>
> <div>{% csrf_token %}</div>
>
> I am sure that there must be a problem with the html but this is driving me
> crazy. Probably some stupid typo.
>
> When I set a breakpoint following the if request.POST the request.FILES is
> an empty dictionary.
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home