如何解决'poster'属性没有与之关联的文件
每次尝试加载主页时,我都会不断收到此错误。这是我从Chrome调试器得到的错误:
The 'poster' attribute has no file associated with it.
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width,initial-scale=1.0">
6 <title>Episode One</title>
7 {% load static %}
8 <link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
9 <link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
10 </head>
11 <body>
During handling of the above exception ('ImageFieldFile' object is not subscriptable),another exception occurred
这是我的模型。py:
class Pilot(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
count = models.IntegerField()
writer = models.CharField(max_length=200)
year = models.IntegerField()
script = models.FileField(blank=True,null=True,upload_to="scripts")
poster = models.ImageField(blank=True,upload_to="posters")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
我的urls.py:
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/',admin.site.urls),path('',include('script_app.urls')),]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
我的settings.py: STATIC_URL ='/ static /'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
STATICFILES_DIR = [
os.path.join(BASE_DIR,'static')
]
我不知道是什么原因引起的。谁能告诉我这是怎么回事?!!!!
解决方法
poster = models.ImageField(blank=True,default='default.png',upload_to="posters")
您会在每次jinja循环尝试解析图像时显示错误,如果他们发现没有任何错误,为防止这种情况,请上载所有图像或设置为默认值。 文件夹中的png ,您就完成了。
,在HTML代码中,{%load static%}总是放在最前面,您将加载静态文件的错误放在了中间。
perl -pi -e 'foreach $key(sort keys %ENV){ s/\$$key/$ENV{$key}/g}' file
然后在setting.py中尝试
{% load static %}
<!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width,initial-scale=1.0">
6 <title>Episode One</title>
8 <link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
9 <link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
10 </head>
11 <body>
尝试我上面所做的事情。它将为您提供帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。