我正在为项目使用Django框架。我正在呈现HTML模板
def index(request):
print(" --- check fine?") # for print debug
trees = Tree.objects.all()
print(trees)
return render(request, "index.html", {'trees': trees}) --- check fine?
<QuerySet [<Tree: Tree object (8)>, <Tree: Tree object (10)>, <Tree: Tree object (11)>, <Tree: Tree object (12)>]>
[06/Feb/2021 17:28:44] "GET / HTTP/1.1" 500 145这些是我的网址。
urlpatterns = [
path("", views.index, name="index"),
path('about/', views.about , name='about'),
path('contact/', views.contact , name='contact'),
path('upload/', views.upload_file , name='upload'),
]DEBUG = False我得到了上面的错误。
当我设置我的DEBUG = True时,一切正常意味着显示index.html,甚至还显示数据。
设置DEBUG = False使查找错误变得困难。
index.html包含以下获取数据的代码。
{% for tree in trees %}
<article class="col-lg-3 col-md-4 col-sm-6 col-12 tm-gallery-item">
<figure>
<img src="{{tree.image.url}}" alt="Image" class="img-fluid tm-gallery-img" />
<figcaption>
<h4 class="tm-gallery-title">{{tree.name}}</h4>
<p class="tm-gallery-description">{{tree.desc}}</p>
<!-- <p class="tm-gallery-price"></p> -->
</figcaption>
</figure>
</article>
{% endfor %}允许的主机是。
ALLOWED_HOSTS = ['*','127.0.0.1','localhost']发布于 2021-02-07 02:15:08
看看这个:
当Django在视图中遇到运行时错误,例如语法错误或视图未能返回Django期望的对象时,
从Django1.5开始,
ALLOWED_HOSTS = []并将其更改为包括Droplet的IP地址和/或指向您的站点的域名。例如:
ALLOWED_HOSTS = "example.com","111.111.111.111“
来源:
发布于 2021-02-07 09:20:35
这是settings.py中的变量
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"由于STATICFILES_STORAGE,collectstatic使用的是白噪声引擎。我评论STATICFILES_STORAGE。我建议删除上述/staticfiles文件夹中的所有STATIC_ROOT目录。
并使用python manage.py collectstatic收集文件。
并在url中使用它。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('ourhouse.urls')),
path('admin/', admin.site.urls),
# path('accounts/',include('accounts.urls'))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)使用静态和媒体文件。
https://stackoverflow.com/questions/66080047
复制相似问题