Upload of file with FileField working in admin but not in template

23 views
Skip to first unread message

Alexander Joseph

unread,
May 3, 2018, 12:44:34 PM5/3/18
to Django users
I'm using CBVs and trying to upload a file with a FileField. It seems to work in admin but not in my template. It doesnt give any errors when creating the record and it saves the rest of the data in the form, but it doesnt save the attachment

Heres my model

class LaserMaskDesign(models.Model):
    laser_mask_design_ui = models.CharField(max_length=60, unique=True) # Wafer Design UI
    mask_layers = models.CharField(max_length=255, default='', blank=True, null=True)
    design_date = models.DateTimeField(blank=True, null=True)
    designer = models.CharField(max_length=255, default='', blank=True, null=True)
    pcm = models.CharField(max_length=255, default='', blank=True, null=True)
    critical_dimensions = models.DecimalField(max_digits=20, decimal_places=4, default=0.0000, blank=True, null=True)
    dimensions = models.CharField(max_length=255, default='')
    thickness = models.DecimalField(max_digits=20, decimal_places=4, default=0.0000, blank=True, null=True)
    material = models.CharField(max_length=255, default='', blank=True, null=True)
    number_of_products = models.IntegerField(default=0, blank=True, null=True)
    chip_list = models.CharField(max_length=255, default='', blank=True, null=True)
    design_document = models.FileField(blank=True, null=True)
    notes = models.TextField(blank=True, null=True, default='')
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
    in_trash = models.BooleanField(default=False)
    inactive_date = models.DateTimeField(blank=True, null=True)

    class Meta:
        ordering = ['laser_mask_design_ui', ]

    def __str__(self):
        return self.laser_mask_design_ui

    def get_absolute_url(self):
        return reverse("engineering:laser_mask_design_detail", kwargs={"pk": self.pk})



and my view

class GaasWaferDesignCreateView(LoginRequiredMixin, CreateView,):
    fields = ("design_ui", "emitting", "contact_location", "optical_power", "design_date", "designer", "design_document", "designer_ui", "in_trash", "inactive_date", "notes")
    model = GaasWaferDesign
    template_name = 'engineering/gaas_wafer_designs/gaas_wafer_design_form.html'

    def form_valid(self, form):
        object = form.save(commit=False)
        object.created_by = self.request.user
        object.save()
        return super(GaasWaferDesignCreateView, self).form_valid(form)


and my template

{% extends "pages/layout.html" %}
{% load static from staticfiles %}
{% load widget_tweaks %}
{% block title %}{% if not form.instance.pk %}Add GaAs Wafer Design{% else %}Edit GaAs Wafer Design - {{ gaaswaferdesign.design_ui }}{{ form.instance.gaas_wafer_design.design_ui }}{% endif %}{% endblock %}
{% load django_bootstrap_breadcrumbs %}

{% block breadcrumb %}
  {% block breadcrumbs %}
      {{ block.super }}
  {% endblock %}
{% endblock %}

{% block content %}
<div class="row">
  <div class="col">
      <h1 class="page_title">{% if not form.instance.pk %}Add GaAs Wafer Design{% else %}Edit GaAs Wafer Design{% endif %}</h1>
  </div>
</div>
<div class="row">
    <div class="col-md-6">
        <div class="card">
          <form method='POST'>
          {% csrf_token %}
          <div class="card-header">
              <strong>GaAs Wafer Design</strong>
              <small>Form</small>
          </div>
          <div class="card-body">
            <p><strong>Bold are required *</strong></p>
            {{ form.non_field_errors }}
            <div class="row">
              <div class="form-group col-sm-12">
                <div class="form-group">
                  <label for="{{ form.design_ui.id_for_label }}"><b>GaAs Wafer Design UI</b></label>
                  {% render_field form.design_ui class+="form-control" %}
                  {{ form.design_ui.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
            <div class="row">
              <div class="form-group col-sm-4">
                <div class="form-group">
                  <label for="{{ form.emitting.id_for_label }}"><b>Emitter Type</b></label>
                  {% render_field form.emitting class+="form-control" %}
                  {{ form.emitting.errors }}
                </div>
              </div>
              <div class="form-group col-sm-4">
                <div class="form-group">
                  <label for="{{ form.contact_location.id_for_label }}"><b>Contact Location</b></label>
                  {% render_field form.contact_location class+="form-control" %}
                  {{ form.contact_location.errors }}
                </div>
              </div>
              <div class="form-group col-sm-4">
                <div class="form-group">
                  <label for="{{ form.optical_power.id_for_label }}"><b>Optical Power</b></label>
                  {% render_field form.optical_power class+="form-control" %}
                  {{ form.optical_power.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
            <div class="row">
              <div class="form-group col-sm-6">
                <div class="form-group">
                  <label for="{{ form.design_date.id_for_label }}"><b>Design Date</b></label>
                  {% render_field form.design_date class+="form-control" %}
                  {{ form.design_date.errors }}
                </div>
              </div>
              <div class="form-group col-sm-6">
                <div class="form-group">
                  <label for="{{ form.designer.id_for_label }}"><b>Designer</b></label>
                  {% render_field form.designer class+="form-control" %}
                  {{ form.owner.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
            <div class="row">
              <div class="form-group col-sm-6">
                <div class="form-group">
                  <label for="{{ form.designer_ui.id_for_label }}">Designer UI</label>
                  {% render_field form.designer_ui class+="form-control" %}
                  {{ form.designer_ui.errors }}
                </div>
              </div>
              <div class="form-group col-sm-6">
                <div class="form-group">
                  <label for="{{ form.design_document.id_for_label }}">Design Document</label>
                  {% render_field form.design_document class+="form-control" %}
                  {{ form.design_document.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
            <div class="row">
              <div class="form-group col-sm-6 text-left">
                <div class="form-group" style="float:left;">
                  <label for="{{ form.in_trash.id_for_label }}">In Trash?</label>
                  {% render_field form.in_trash class+="form-control" %}
                  {{ form.in_trash.errors }}
                </div>
              </div>
              <div class="form-group col-sm-6">
                <div class="form-group">
                  <label for="{{ form.inactive_date.id_for_label }}">Date Trashed</label>
                  {% render_field form.inactive_date class+="form-control" %}
                  {{ form.inactive_date.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
            <div class="row">
              <div class="form-group col-sm-12">
                <div class="form-group">
                  <label for="{{ form.comments.id_for_label }}">Notes</label>
                  {% render_field form.notes class+="form-control" %}
                  {{ form.notes.errors }}
                </div>
              </div>
            </div>
            <!--/.row-->
          </div>
          <div class="card-footer">
            <input type="submit" class="btn btn-primary" value="Save">
        </div>
      </form>
    </div>
  </div>
</div>
{% endblock %}


Thanks!

Andréas Kühne

unread,
May 3, 2018, 2:36:27 PM5/3/18
to django...@googlegroups.com
Hi,

The problem isn't CBV's, it's probably your template.

You have the following:
 <form method='POST'>

But it should be:
 <form method='POST' enctype="multipart/form-data">

Otherwise the browser doesn't know how to encode the files....

Regards,

Andréas

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/981c16a8-29e6-46e1-b539-7511c50b8849%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
May 3, 2018, 3:51:55 PM5/3/18
to django...@googlegroups.com
That was it, thank you!

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/aBYVbKp82lA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
Best Regards,

Alexander Joseph

Reply all
Reply to author
Forward
0 new messages