Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django dict最多期望1个参数,得到3个错误_Django - Fatal编程技术网

Django dict最多期望1个参数,得到3个错误

Django dict最多期望1个参数,得到3个错误,django,Django,我有一个叫Picture的模型,它有两个imagefield,一个叫Image,一个叫small。我有2个imagefield的主要原因是,我的模型下的函数将根据它们的位置调整图像大小并保存它 我的主要目标是最小化不需要的代码,因为我有两个重复的函数,它们执行相同的操作,但用于不同的字段。我们可以创建一个函数来执行相同的操作,但用于不同的字段,而不是使用2个函数 class Picture(models.Model): small = models.ImageField(upload_

我有一个叫Picture的模型,它有两个imagefield,一个叫Image,一个叫small。我有2个imagefield的主要原因是,我的模型下的函数将根据它们的位置调整图像大小并保存它

我的主要目标是最小化不需要的代码,因为我有两个重复的函数,它们执行相同的操作,但用于不同的字段。我们可以创建一个函数来执行相同的操作,但用于不同的字段,而不是使用2个函数

class Picture(models.Model):

    small = models.ImageField(upload_to="small/",blank=True)
    image = models.ImageField(upload_to="images/",blank=True)

    def save(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.image.width
        ph = self.image.height
        mw = 500
        mh = 500

        if (pw > mw) or (ph > mh):
            filename = str(self.image.path)
            imageObj = img.open(filename)
            ratio = 1

            if ( pw > mw):
                ratio = mw / float(pw)
                pw = mw
                ph = int(math.floor(float(ph)* ratio))
            if ( ph > mh):
                ratio = ratio * ( mh /float(ph))
                ph = mh 
                pw = int(math.floor(float(ph)* ratio))

            imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
            imageObj.save(filename)

    def small(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.small.width
        ph = self.small.height
        mw = 1500
        mh = 1500

        if (pw > mw) or (ph > mh):
            filename = str(self.small.path)
            imageObj = img.open(filename)
            ratio = 1

            if ( pw > mw):
                ratio = mw / float(pw)
                pw = mw
                ph = int(math.floor(float(ph)* ratio))
            if ( ph > mh):
                ratio = ratio * ( mh /float(ph))
                ph = mh 
                pw = int(math.floor(float(ph)* ratio))

            imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
            imageObj.save(filename)
我试图做的是创建一个名为Resizer的类,该类带有字段的参数。因此,如果我上传一个小字段中的图像,我的模型将使用if语句确定它是否小。转换为图像变量并调用Resizer类

问题是我明白了

   error dict expect at most 1 argument , got 3
我的模特

from pet.fields import Resizier

class Picture(models.Model):

    small = models.ImageField(upload_to="small/",blank=True)
    image = models.ImageField(upload_to="images/",blank=True)

    if image:
        Resizier(image)
    else:
        small = image
        Resizier(image)
我的田里

from PIL import Image as img
import math

image = {}
class Resizier(image):


    def save(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.image.width
        ph = self.image.height
        mw = 400
        mh = 400

        if (pw > mw) or (ph > mh):
            filename = str(self.image.path)
            imageObj = img.open(filename)
            ratio = 1

            if ( pw > mw):
                ratio = mw / float(pw)
                pw = mw
                ph = int(math.floor(float(ph)* ratio))
            if ( ph > mh):
                ratio = ratio * ( mh /float(ph))
                ph = mh 
                pw = int(math.floor(float(ph)* ratio))

            imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
            imageObj.save(filename)
错误

    from pet.fields import Resizier
  File "C:\o\17\mysite\pet\fields.py", line 5, in <module>
    class Resizier(image):
TypeError: Error when calling the metaclass bases
    dict expected at most 1 arguments, got 3
从pet.fields导入大小调整器
文件“C:\o\17\mysite\pet\fields.py”,第5行,在
类大小调整器(图像):
TypeError:调用元类基时出错
dict最多需要1个参数,得到3个
您的问题在于:

from PIL import Image as img
import math

image = {}
class Resizier(image): # you are inheriting from `image`, which is a dict,
                       # you probably want `Picture` here.
然而,你也有一些其他的问题。我是否可以建议,与其尝试重新发明轮子,不如使用已有的东西。以下是提供此功能的一个示例。

您的问题在于:

from PIL import Image as img
import math

image = {}
class Resizier(image): # you are inheriting from `image`, which is a dict,
                       # you probably want `Picture` here.

然而,你也有一些其他的问题。我是否可以建议,与其尝试重新发明轮子,不如使用已有的东西。下面是一个提供此功能的函数。

请不要发布您发布的所有代码,只需发布完整的错误
python
显示(堆栈跟踪,我们需要查看)和包含错误代码的函数,根据文件:line reportedOkay posted at the bottomPlease,而不是您发布的所有代码,只需发布完整的错误
python
显示(堆栈跟踪,我们需要看到它)和包含错误代码的函数,根据底部发布的文件:line reportedOkay什么样的问题,你能解释得更进一步一点吗?你的意思是我的调整大小功能是否会缩放?什么样的问题,你能解释得更进一步一点吗?你的意思是我的调整大小功能是否会缩放?