【问题标题】:django TemplateDoesNotExist, generated urlsdjango TemplateDoesNotExist,生成的 url
【发布时间】:2014-12-29 19:31:36
【问题描述】:

我需要通过标题创建链接并加载每个链接生成的模板noticia_detail.html,但我有一个问题如下:

我正在寻找错误,我认为问题出在设置文件、模板路径或其他东西上,请帮助我

对于我请求的 slug 生成的每个 URL,它都会抛出:

TemplateDoesNotExist at /noticia/crea-horario/
infogeneral/noticia_detail.html
Request Method: GET
Request URL:    http://localhost:1280/noticia/crea-horario/
Django Version: 1.7.1
Exception Type: TemplateDoesNotExist
Exception Value:    
infogeneral/noticia_detail.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in select_template, line 194
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.5
Python Path:    
['C:\\Users\\PC\\Documents\\python\\taskalertproyect\\taskalert',
 'C:\\Python27\\lib\\site-packages\\setuptools-1.4.2-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django_fiber-0.11.2-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\easy_thumbnails-1.4-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\djangorestframework-2.3.8-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django_compressor-1.3-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\pillow-2.2.1-py2.7-win-amd64.egg',
 'C:\\Python27\\lib\\site-packages\\six-1.5.2-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django_appconf-0.6-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\pyopenssl-0.14-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']
Server time:    Lun, 3 Nov 2014 01:10:43 -0500

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\home\taskalertproyect\taskalert\infogeneral\templates\infogeneral\noticia_detail.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\infogeneral\noticia_detail.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\infogeneral\noticia_detail.html (File does not exist)
C:\Users\PC\Documents\python\taskalertproyect\taskalert\infogeneral\templates\infogeneral\noticia_detail.html (File does not exist)
C:\Python27\lib\site-packages\django_markdown\templates\infogeneral\noticia_detail.html (File does not exist)

index.html

<section class="content">
                {% for noticia in pagina.noticia.all %}
                    <article>
                        <div class="post">
                            <h1 class="title">
                                <a href="/noticia/{{noticia.slug}}/">{{ noticia.titulo_noticia}}</a>
                            </h1>
                            <p>{{ noticia.contenido_noticia|safe }}<p>
                            <a class="read_more" href="/noticia/{{noticia.slug}}/">Continue Reading <i class="read_more_arrow"></i> </a>
                        </div>
                    </article>
                {% endfor %}
            </section><!-- Content End -->

noticia_detail.html

<h2>{{ object.titulo_noticia }}</h2>
<p>{{ object.contenido_noticia }}</p>

urls.py

from infogeneral.views import NoticiaDetailView
url(r'^noticia/(?P<slug>[-\w]+)/$', NoticiaDetailView.as_view(),name='noticia_detail')

models.py

class Noticia(models.Model):
    titulo_noticia = models.CharField(max_length=100)
    slug = models.SlugField(null=True, editable=False)

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.titulo_noticia)
        super(Noticia, self).save(*args, **kwargs)

views.py

from django.views.generic import ListView, DetailView
from .models import Noticia

class NoticiaDetailView(DetailView):
    tamplate_name = 'noticia_detail.html'
    context_object_name = 'noticias'
    model = Noticia

settings.py

TEMPLATES_URL = '/templates/'

TEMPLATE_DIRS = (
    'C:/Users/PC/Documents/python/taskalertproyect/taskalert/infogeneral/templates',
)

【问题讨论】:

  • 当您在 Windows 机器上并且您的项目位于“c:/Users/PC/Documents/python/taskalertproyect”时,为什么要使用 Linux“/home/taskalertproyect...”路径?
  • 感谢并应用更改,但还是一样

标签: python django url


【解决方案1】:

您的模板无法加载。 Django 尝试从错误消息底部给出的位置加载它,但在那里找不到它。确保模板位于正确的位置。根据错误消息,正确的位置将在C:\Users\PC\Documents\python\taskalertproyect\taskalert\infogeneral\templates\infogeneral\noticia_detail.html

您的设置似乎也反映了 UNIX/Linux 系统,但输出显示为 Windows。您应该更新路径。

【讨论】:

  • ty,我更改了所有目录但我没有匹配任何结果,我会检查路径。
猜你喜欢
  • 2011-06-13
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多