3

我有一个包含 11 个字段和一个图像上传字段的表单。填写并点击提交后,我收到一个 TypeError 说需要一个整数。如果我删除图像字段,则不会出现此错误。检查图像目录,在引发错误之前将文件保存到服务器。

表格.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")

视图.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))

这是确切的错误代码:

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

编辑:根据要求:

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

设置.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'
4

2 回答 2

4

在您的 settings.py 脚本中,您有以下行:

FILE_UPLOAD_PERMISSIONS '0760'

它不应该有引号。当它需要一个整数时,您试图用一个字符串作为第二个参数调用 os.chmod() 。具体来说,八进制权限值。在 python 中,带有前导 0 的数字被自动认为是八进制值并转换为整数。因此,只需使用此行:

FILE_UPLOAD_PERMISSIONS = 0760

顺便说一句,你为什么不在 settings.py 中使用等号?

于 2012-11-12T02:32:36.187 回答
0

上述问题的更新解决方案只需在您的 settings.py 文件中添加此代码段。

FILE_UPLOAD_PERMISSIONS = 0o760

请记住,您必须使用o.

于 2019-06-11T17:45:29.637 回答