Django不能完全找到正确的路径(Django can't quite get the right path)

编程入门 行业动态 更新时间:2024-10-27 14:28:55
Django不能完全找到正确的路径(Django can't quite get the right path)

我在这里有这个观点:

from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect, HttpResponse from django.urls import reverse from django.template import loader from django.views.generic import ListView class IndexView(ListView): def get(self, request): template_name = loader.get_template('home/blog.html') return render(request, template_name) return HttpResponse(template_name.render(request))

因此,如果我删除返回渲染(request,template_name),一切都将正常工作,但现在当我尝试使用返回渲染(request,template_name)时,我收到以下错误:

<django.template.backends.django.Template object at 0x7f13916f46d8>

Django试图找到模板的地方:

django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/miranda/home/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist) django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/env/lib/python3.4/site-packages/django/contrib/admin/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist) django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/env/lib/python3.4/site-packages/django/contrib/auth/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist)

我该怎么办? 现在是时候说我已经开始这个项目我有路径问题,半小时前由于某种原因Django在我的虚拟环境的文件夹中搜索。

I have this view here:

from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect, HttpResponse from django.urls import reverse from django.template import loader from django.views.generic import ListView class IndexView(ListView): def get(self, request): template_name = loader.get_template('home/blog.html') return render(request, template_name) return HttpResponse(template_name.render(request))

So if I remove return render(request, template_name) everything will be working properly but now when I trying to use return render(request, template_name) I get these error:

<django.template.backends.django.Template object at 0x7f13916f46d8>

places where Django tried to find the template:

django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/miranda/home/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist) django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/env/lib/python3.4/site-packages/django/contrib/admin/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist) django.template.loaders.app_directories.Loader: /home/marton/documents/github/fmi-fall-2016/django/click_bait/env/lib/python3.4/site-packages/django/contrib/auth/templates/<django.template.backends.django.Template object at 0x7f13916f46d8> (Source does not exist)

What should I do? Now is the time to say that eversince I have started this project I have had problem with paths, half an hour ago for some reason Django was searching in my virtual environment's folder.

最满意答案

你在这里误解了一些事情。 加载器返回一个模板对象,而不是模板名称。 你根本不需要调用loader.get_template ; 你直接传递模板路径来渲染:

return render(request, 'home/blog.html')

You are misunderstanding a few things here. The loader returns a template object, not a template name. You don't need that call to loader.get_template at all; you pass the template path directly to render:

return render(request, 'home/blog.html')

更多推荐

本文发布于:2023-07-05 07:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   正确   Django   path

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!