【问题标题】:django template "file name too long"django模板“文件名太长”
【发布时间】:2010-10-21 13:53:06
【问题描述】:

我对 Python 和 Django 非常陌生,所以也许有人可以为我指明正确的方向。

我有以下 url.py 行

      url(r'^$', direct_to_template,
                  {'template':'index.html',
                  'extra_context':{'featured_actors': lambda: User.objects
                                    .annotate(avatars_nb=Count('avatar'))
                                    .filter(actor_profile__is_featured=True, avatars_nb__gt=0)
                                    .order_by('?')[:4]},
                 }, name='index'),

这一切在很长一段时间内都运行良好,但我无缘无故地看到我突然收到此模板错误。

 TemplateSyntaxError at /
 Caught an exception while rendering: (36, 'File name too long')

在第 70 行

 66   {% if featured_actors|length %}
 67       <div id="featured"> 
 68         <h2>Featured Actors: </h2>
 69         <ul>
 70             {% for actor in featured_actors %}
 71             <li> 
 72                 <a href="{% url public_profile actor.username %}">
 73                     <img src="{% avatar_itself_url actor.avatar_set.all.0 200 %}" alt="{{ actor.profile.firstname }} {{ actor.profile.lastname }}" style="max-width:140px" height="200"/> 
 74                 </a>
 75             </li>
 76             {% endfor %}

调试此问题的最佳方法是什么?

更新

 126     def avatar_url(self, size):
 127         return self.avatar.storage.url(self.avatar_name(size))

我想我发现了一点问题,其中一个用户配置文件也出现了同样的错误。所以我认为这一定是他的头像/图像路径太长了。我正在尝试缩小范围...

【问题讨论】:

    标签: python django django-urls


    【解决方案1】:

    可能是图片路径{% avatar_itself_url actor.avatar_set.all.0 200 %} 太长。能不能把&lt;img ...的那行删掉,看看模板是否渲染?

    如果上面的渲染,来自python manage.py shell,你能验证你的图像路径的长度吗?长度是否大于 255 个字符?

    回复评论

    你的图片路径太长了,我的意思是:

    <img src="/this/is/a/very/long/path/which/exceeds/255/characters/something.png" />
    

    上面不是 255 个字符长,但你明白了。上面的 src 路径可能很长。试着找出那条路径是什么并计算它的长度。 avatar_itself_url 的实现是什么样的? Avatar的unicode怎么样?它返回什么?你有一个名字很长的头像吗?

    复制错误消息

    这是从 python 复制错误消息的方法。在 python 脚本中运行以下命令:

    long_filename = 'a' * 256
    fp = open(long_filename, 'w')
    fp.close()
    

    上面应该返回消息:IOError: [Errno 36] File name too long:

    显示图片路径

    你能用它的内容替换html中的img标签吗:{% avatar_itself_url actor.avatar_set.all.0 200 %}?您应该看到图像的路径,而不是看到图像。目测超过 256 个字符的字符应该不是问题。

    【讨论】:

    • 好的,正如你所建议的那样,我删除了 img 行以查看页面是否呈现并且确实如此。那么这到底是什么意思。怎么测试看看哪个item太长了?有没有办法让我调试它?
    • 255的限制在哪里设置?
    • 据我所知,没有一条通向头像的路径接近 255 个字符。
    • 我想我在问一个关于 django 的更基本的问题。在我的视图/模板中,我有什么方法可以在遇到问题之前将太长的文件名打印到屏幕上?顺便说一句,谢谢。
    猜你喜欢
    • 2021-08-14
    • 2014-05-30
    • 2015-04-18
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多