django 模板“文件名太长”

发布于 2024-09-28 14:53:06 字数 1551 浏览 3 评论 0 原文

我对 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))

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

I'm very new to Python and Django so maybe someone can point me in the right direction.

I have the following url.py line

      url(r'^

All this was working perfectly fine for a long time but for no reason that I can see all of a sudden I'm getting this template error.

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

On line 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 %}

What is the best way to debug this?

UPDATE

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

I think I found a bit of the problem, one of the user profiles is also giving the same error. So I think it must be a avatar/image path for him that is too long. I'm trying to narrow it down...

, 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'),

All this was working perfectly fine for a long time but for no reason that I can see all of a sudden I'm getting this template error.


On line 70


What is the best way to debug this?

UPDATE


I think I found a bit of the problem, one of the user profiles is also giving the same error. So I think it must be a avatar/image path for him that is too long. I'm trying to narrow it down...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-10-05 14:53:06

图片路径 {% avatar_itself_url actor.avatar_set.all.0 200 %} 可能太长。您可以删除带有 的行并查看模板是否呈现吗?

如果上面的内容从 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 个字符的内容应该不成问题。

It's possible that the image path {% avatar_itself_url actor.avatar_set.all.0 200 %} is too long. Can you delete the line with <img ... and see if the template renders?

If the above renders, from the python manage.py shell, can you verify the length of your image path? Is the length greater than 255 chars?

ANSWER TO COMMENT

Your image path is too long, by that I mean:

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

The above is not 255 chars long but you get the idea. The above src path might be very long. Try to find out what that path is and calculate its length. What does the implementation of avatar_itself_url look like? How about the unicode of Avatar? What does it return? Do you have an Avatar with a very long name?

Replicating the error msg

Here's how you can replicate the error msg from python. Run the following in a python script:

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

The above should return the msg: IOError: [Errno 36] File name too long:.

Displaying the image path

Can you replace the img tag from the html by just its content: {% avatar_itself_url actor.avatar_set.all.0 200 %}? Instead of seeing the image, you should see the path of the image. Eyeballing the one longer than 256 chars shouldn't be an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文