'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, null=True, upload_to="posters")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

我的网址.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)

我的设置.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%}总是位于顶部,而您将其放在中间,这是加载静态文件的错误之处。

{% 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>

在设置中。请尝试这个

STATIC_URL = '/static/'
STATIC_ROOT= os.path.join(BASE_DIR, "static", "static_root")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

试试我上面所做的。它会帮助你的。

相关内容

最新更新