0

I'm using ModelForms with the generic editing views (create, update, delete). One of my aims in this exercise is to get to know some frameworks (Django + Bootstrap + a plugin (e.g. PDF.js)) and use as little custom code as possible.

I can create a 'document' record through the Admin site and the upload is saved in the 'files' location I set. When I try through my site, no file is uploaded, although the other fields will be updated (evidently the form validates). (The FileField IS nullable, because I want to be able to have a pointer to a non-digital asset). The relevant code:

    # models.py
class document(models.Model):
    ref_file = models.FileField(upload_to='documents/', blank=True, null=True)

    def get_absolute_url(self):
        return reverse('knowledge_manager:doc_detail', kwargs={'pk': self.pk})

# urls.py
urlpatterns = [
    url(r'^doc$', views.docIndex.as_view(), name='doc_index'),
    url(r'^doc/(?P<pk>\w+)/det', views.docUpdate.as_view(), name='doc_detail'),
    url(r'^doc/new/$', views.docCreate.as_view(), name='doc_create'),
    url(r'^doc/(?P<pk>\w+)/update', views.docUpdate.as_view(), name='doc_update'),
    url(r'^doc/(?P<pk>\w+)/del', views.docDelete.as_view(), name='doc_delete')

]

# views.py
class docIndex(generic.ListView):
    model = document
    template_name = 'knowledge_manager/_index.html'
    context_object_name = 'document_set'

class doc_detail(generic.DetailView):
    model = reference
    template_name = 'knowledge_manager/doc_detail.html'
    context_object_name = 'document'
    form_class = doc_form
    success_url = reverse_lazy('knowledge_manager:doc_index')

class docCreate(generic.CreateView):
    model = document
    template_name = 'knowledge_manager/doc_detail.html'
    form_class = doc_form
    success_url = reverse_lazy('knowledge_manager:doc_index')

class docUpdate(generic.UpdateView):
    model = document
    template_name = 'knowledge_manager/doc_detail.html'
    context_object_name = 'document'
    form_class = doc_form
    success_url = reverse_lazy('knowledge_manager:doc_index')

class docDelete(generic.DeleteView):
    model = document
    success_url = reverse_lazy('knowledge_manager:doc_index')

# forms.py
class doc_form(ModelForm):
    class Meta:
        model = document
        fields = '__all__'

Questions:

  1. what do you think is going wrong? Surely ModelForms with FileFields initialise using 'request.FILES' as well as 'instance' and 'request.POST'?
  2. what's a good way to get debugging messages about what is actually getting posted? Using function views I used to just pop 'print('made it to here, value X is', value_x) - is there a better way than just overloading the standard functions of ModelForms etc?
Atcrank
  • 439
  • 3
  • 11

1 Answers1

0

I had overlooked this solution (stackoverflow), which pointed out that the form needs this tag to correctly send material to request.FILES.

<form action="" method="post" **enctype="multipart/form-data"**>
Atcrank
  • 439
  • 3
  • 11