简体   繁体   English

在form.save上Django“TypeError:一个整数是必需的”

[英]Django “TypeError: an integer is required” on form.save

I have a form with 11 fields and one image upload field. 我有一个包含11个字段和一个图像上传字段的表单。 After filling in and hitting submit, I'm given a TypeError saying that an integer is required. 填写并点击提交后,我收到一个TypeError,说明需要一个整数。 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: 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: 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: 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: 在settings.py脚本中,您有以下行:

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. 当你需要一个整数时,你试图用一个字符串作为第二个参数调用os.chmod()。 Specifically, the octal permissions value. 具体来说,八进制权限值。 In python, numbers with a leading 0 are automatically considered to be octal values and are converted to integers. 在python中,带前导0的数字自动被视为八进制值并转换为整数。 So, just use this line instead: 所以,只需使用此行:

FILE_UPLOAD_PERMISSIONS = 0760

By the way, why aren't you using equals signs in your settings.py? 顺便问一下,你为什么不在settings.py中使用equals标志?

Updated Solution to the above problem isjust add this snippet in your settings.py file. 针对上述问题的更新解决方案只需在settings.py文件中添加此代码段即可。

FILE_UPLOAD_PERMISSIONS = 0o760

Keep it in mind that you have to use o . 请记住,你必须使用o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM