Reputation: 213
I'm working on an app that will let users build their own databases, and one of the fields they can create is a photo field. I'm using a form model, and while it captures the rest of the data, it won't try to grab the files.
I have file uploads working elsewhere on the site, and if I upload the file manually in the admin panel it shows up just fine, but the form itself won't grab the file.
In my template, I have:
<form action="/orgs/{{org.id}}/db/{{db.unique_id}}/rows/add/" method="post" enctype="multipart/form-data">
{% csrf_token %}
...
{% elif column.field_type = 23 %}
{{form.photofield}}
{% endif %}
</form>
In my models:
from django.db import models
def get_upload_file_name(instance, filename):
return settings.UPLOAD_FILE_PATTERN % (str(time()).replace('.','_'), filename)
class DbField(models.Model):
...
photofield = models.FileField(upload_to=get_upload_file_name, null=True, blank=True)
in my forms:
from django import forms
from byodb.models import DbField
class DbFieldForm(forms.ModelForm):
class Meta:
model = DbField
fields = ('photofield',)
widgets = {'photofield': forms.FileInput()}
and in my views:
def org_database_add_row(request, organization_id=1, db_id=1):
if request.POST:
if request.POST.get(str(column.unique_id)):
if column.field_type == 23:
form = DbFieldForm(request.POST, request.FILES)
if form.is_valid():
f = form.save(commit=False)
...
f.save()
sorry for any confusion about column/row stuff, it's just data to place the field. If you need more detail then I can add more, but on the surface it looks like everything SHOULD work...
ALTERNATIVELY, I have been trying to get it to work another way, avoiding using the form altogether so that the file field will be named the same as my column.
In that instance, in my template it reads:
<input type="field" name="{{column.unique_id}}" id="photofield" />
and in my views it reads:
elif column.field_type == 23:
DbField.objects.create(row=row, column=column, unique_id=column.unique_id, creator=request.user, last_editor=request.user, field_type=column.field_type, photofield=request.FILES[str(column.unique_id)])
However it's the same issue, it will create the field just fine, but it won't try to grab the file. I'm not sure why it's only failing here, as this works everywhere else on the site.
Upvotes: 0
Views: 90
Reputation: 213
My bad, I figured out the problem.
In my views, where I had:
if request.POST:
if request.POST.get(str(column.unique_id)):
if column.field_type == 23:
DbField.objects.create(row=row ... photofield=request.FILES[str(column.unique_id)])
It was failing because request.POST.get(str(column.unique_id)) was empty since it was FILES and not POST.
I rewrote the view to accomodate:
if request.POST.get(str(column.unique_id)):
...
else:
if request.FILES[str(column.unique_id)]:
if column.field_type == 23:
DbField.objects.create(row=row ... photofield=request.FILES[str(column.unique_id)])
else:
DbField.objects.create(row=row ... all other fields left blank)
This way if the request.POST for the field comes up empty, it checks to see if there's a file attached and the correct field type, if not then it just creates the empty field.
Upvotes: 1