每次我试图加载我的主页时,我都会得到这个错误。这是我从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这是我的models.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)我的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)我的静态: STATIC_URL =‘/settings.py/’
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]我想不出是什么地方出了问题。谁能告诉我哪里出了问题?!
发布于 2020-08-17 11:30:10
poster = models.ImageField(blank=True, default='default.png', upload_to="posters")每次jinja循环尝试解析图像时,您都会被显示错误,如果他们发现没有显示任何内容并阻止这种情况,您可以上传所有图像或在您的posters文件夹中设置default.png,您就完成了。
发布于 2020-08-17 12:55:31
在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>在setting.py中试试这个
STATIC_URL = '/static/'
STATIC_ROOT= os.path.join(BASE_DIR, "static", "static_root")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]尝试一下我上面所做的。它会帮到你的。
https://stackoverflow.com/questions/63444053
复制相似问题