2010-10-21 115 views
0

我對Python和Django非常陌生,所以也許有人可以指出我正確的方向。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 %} 

什麼是調試的最佳方式?

UPDATE

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

我想我找到了一點問題,用戶配置文件之一也給予同樣的錯誤。所以我認爲這對他來說太長了一定是他的化身/形象。我試圖縮小它的範圍...

回答

2

圖像路徑{% avatar_itself_url actor.avatar_set.all.0 200 %}可能太長。你可以用<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腳本中運行以下代碼:

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

上面應該返回msg:IOError: [Errno 36] File name too long:

顯示圖像路徑

你可以通過不僅僅是其內容替換從HTML img標籤:{% avatar_itself_url actor.avatar_set.all.0 200 %}?而不是看到圖像,你應該看到圖像的路徑。目光超過256個字符的應該不是問題。

+0

好,如你所建議的,我刪除了img行,看看頁面是否呈現,並確實如此。那究竟是什麼意思呢。如何測試看看什麼項目太長?有沒有辦法讓我調試? – 2010-10-21 16:18:07

+0

無論如何,255組的限制在哪裏? – 2010-10-21 16:45:40

+0

據我所見,沒有任何通向頭像的路徑接近255個字符。 – 2010-10-21 16:50:10