I have a form with 11 fields and one image upload field. After filling in and hitting submit, I'm given a TypeError saying that an integer is required. this error doesn't come up if i remove the imagefield. checking the image directory, the files are saved to the server before the error is thrown.
Forms.py:
class ListingForm(ModelForm):
seller_email = forms.EmailField(max_length="", required=True, widget=forms.TextInput(attrs={'placeholder': "jsmith@stevens.edu"}))
title = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder':"The Cat in the Hat"}))
edition = forms.IntegerField(required=True, widget=forms.TextInput(attrs={'placeholder':"5"}))
author = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder':"Dr. Seuss"}))
isbn = forms.CharField(label="ISBN", required=True, widget=forms.TextInput(attrs={'placeholder':"9780717260591"}))
price = forms.FloatField(required=True, widget=forms.TextInput(attrs={'type':"number", 'placeholder':"50.00"}))
classcode= forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder':"MA115"}))
#description = forms.CharField(widget=forms.TextArea(attrs={'placeholder':"You should buy this because..."}))
condition = forms.ChoiceField(required=True, widget=forms.Select(), choices=([(4,"New"), (3,"Good"), (2,"Fair"), (1,"Poor")]))
seller_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder':"John Doe"}))
seller_phone = forms.CharField(widget=forms.TextInput(attrs={'placeholder':"(555) 555-1234"}))
picture = forms.ImageField(widget=forms.FileInput('accept':"image/*"))
#picture = forms.ImageField()
uuid = forms.CharField(widget=forms.HiddenInput(), initial=uuid.uuid4())
timeposted = forms.CharField(widget=forms.HiddenInput(), initial=datetime.now())
sale_status = forms.BooleanField(widget=forms.HiddenInput(), initial=True)
class Meta:
model = Listing
fields = ('seller_email', 'title', 'edition', 'author', 'isbn', 'price', 'classcode', 'description', 'condition', 'seller_name', 'seller_phone', 'picture', 'uuid', 'timeposted', 'sale_status')
#exclude = ("uuid", "timeposted", "sale_status", "picture", "school")
Views.py:
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import Context, loader, RequestContext
from django.contrib.auth.models import User
from cbt.accounts.models import School, UserProfile
from cbt.listings.models import Listing
from django.contrib import auth
from django.core.context_processors import csrf
from django.core.mail import send_mail
from django.core.files import File
from datetime import datetime
from cbt.settings import MEDIA_ROOT
from cbt.listings.forms import ListingForm
school_list = School.objects.all().order_by('name')
def add(request):
form = ListingForm()
if request.method == 'POST':
form = ListingForm(request.POST, request.FILES)
if form.is_valid() and form.is_multipart():
form.save()
return HttpResponseRedirect(reverse('cbt.views.home'))
return render_to_response('listings/add.html', {'school_list':school_list, 'form':form}, context_instance=RequestContext(request))
Here is the exact error code:
Environment:
Request Method: POST
Request URL: http://alligator.collegebooktrade.info/listings/add/
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'cbt.accounts',
'cbt.listings')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/cbt/cbt/listings/views.py" in add
25. form.save()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save
364. fail_message, commit, construct=False)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_instance
86. instance.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
1576. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
909. for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in as_sql
872. for obj in self.query.objs
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save
249. file.save(file.name, file, save=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save
86. self.name = self.storage.save(name, content)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in save
45. name = self._save(name, content)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in _save
212. os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
Exception Type: TypeError at /listings/add/
Exception Value: an integer is required
EDIT: As requested:
Model:
class Listing(models.Model):
title = models.CharField(max_length="100")
edition = models.CharField(max_length="5")
author = models.CharField(max_length="50")
isbn = models.CharField(max_length="13")
price = models.FloatField()
classcode = models.CharField(max_length="12")
description = models.CharField(max_length="400")
condition = models.CharField(max_length="10")
#school = models.OneToOneField(School)
school = models.CharField(max_length="36")
picture = models.ImageField(upload_to="listings")
#CONTACT INFO:
seller_name = models.CharField(max_length="30")
seller_phone = models.CharField(max_length="11")
seller_email = models.EmailField(max_length="75")
uuid = models.CharField(max_length="36")
timeposted = models.DateTimeField()
sale_status = models.BooleanField() #0 for sale, 1 for sold
Settings.py:
#just including pertinent settings...
FILE_UPLOAD_PERMISSIONS '0760'
FILE_UPLOAD_HANDLERS ('django.core.files.uploadhandler.MemoryFileUploadHandler', 'django.core.files.uploadhandler.TemporaryFileUploadHandler')
MIDDLEWARE_CLASSES ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware')
FILE_UPLOAD_MAX_MEMORY_SIZE '2621440'
In your settings.py script, you have this line:
FILE_UPLOAD_PERMISSIONS '0760'
It should not have quotes around it. You are trying to call os.chmod() with a string as the second argument when it expects an integer. Specifically, the octal permissions value. In python, numbers with a leading 0 are automatically considered to be octal values and are converted to integers. So, just use this line instead:
FILE_UPLOAD_PERMISSIONS = 0760
By the way, why aren't you using equals signs in your settings.py?