Python 尝试上载django中带有ImageField的图像时出现TypeError

Python 尝试上载django中带有ImageField的图像时出现TypeError,python,django,image,types,upload,Python,Django,Image,Types,Upload,我在尝试上载带有ImageField的图像时收到此错误: TypeError at /admin/article/articulo/7/change/ not all arguments converted during string formatting Request Method: POST Request URL: http://127.0.0.1:8000/admin/article/articulo/7/change/ Django Version: 2.1.5 Excepti

我在尝试上载带有ImageField的图像时收到此错误:

TypeError at /admin/article/articulo/7/change/
not all arguments converted during string formatting
Request Method: POST
Request URL:    http://127.0.0.1:8000/admin/article/articulo/7/change/
Django Version: 2.1.5
Exception Type: TypeError
Exception Value:    
not all arguments converted during string formatting
Exception Location:  
C:\Users\HOME\ProyectosDjango\weMarket\apps\article\models.py in 
upload_location, line 6
def upload_location(instance, filename):
    return "static/img/" %(instance.id, filename)

class Articulo(models.Model):

...

nombre_imagen=models.ImageField(upload_to=upload_location,
    null=True, blank=True, 
    width_field="width_field", 
    height_field="height_field")
width_field=models.IntegerField(default=0)
height_field=models.IntegerField(default=0)

...

def __str__(self):
    return "("+str(self.id)+") " + self.nombre_producto
这是models.py中带有ImageField的部分:

TypeError at /admin/article/articulo/7/change/
not all arguments converted during string formatting
Request Method: POST
Request URL:    http://127.0.0.1:8000/admin/article/articulo/7/change/
Django Version: 2.1.5
Exception Type: TypeError
Exception Value:    
not all arguments converted during string formatting
Exception Location:  
C:\Users\HOME\ProyectosDjango\weMarket\apps\article\models.py in 
upload_location, line 6
def upload_location(instance, filename):
    return "static/img/" %(instance.id, filename)

class Articulo(models.Model):

...

nombre_imagen=models.ImageField(upload_to=upload_location,
    null=True, blank=True, 
    width_field="width_field", 
    height_field="height_field")
width_field=models.IntegerField(default=0)
height_field=models.IntegerField(default=0)

...

def __str__(self):
    return "("+str(self.id)+") " + self.nombre_producto
如果需要,这是表格中的一部分:

class ArticleForm(forms.ModelForm):

class Meta:
    model = Articulo

    fields = [
        'nombre_imagen',
        'nombre_producto',
        'id_clasificacion_fk',
        'Descripcion',
        'long_descripcion',
        'precio',
        'cantidad',
        ]
以及带有以下表单的HTML:

<div class="row">
<div class="col-md-10">
    <form method="post">
        <h5 class="mb-3">Agregar artículo</h5>
            {% csrf_token %}
            {{ form.as_p }}
        <button type="submit" class="btn btn-lg btn-success">Guardar 
cambios</button>
    </form>
</div>
<div class="col-md-2">
    <img src="{% static 'img/handpen.png'%}"width="350px" height="310px" 
/>
</div>
</div>

阿格雷加艺术中心
{%csrf_令牌%}
{{form.as_p}}
瓜达尔
坎比奥斯
如果有人能帮我,请告诉我


谢谢你

您的
上传位置
功能出错。您正在尝试格式化字符串,但未包含“替换”标记

应该是

def upload_location(instance, filename):
    return "static/img/%s/%s/" % (instance.id, filename)

是的,你是对的!但我还有一个问题,当我试图上传文件时,它会显示“nombre_imagen属性没有与之关联的文件”,并且它不会显示图像。当我在DB中看到nombre_imagen时,里面没有路径。可能有什么问题?在html表单中,您通常必须添加enc_类型以上载文件。