1

我目前正在 Django 1.3.1 上做一些开始的事情,并且以下错误困扰了我 2 个小时。帮助我找出错误。我已将我的代码包含在bitbucket上。

错误:-

TemplateSyntaxError at /events/archive/
Invalid block tag: 'else', expected 'empty' or 'endfor'

模板错误

在模板 /home/virus/py_tut/startthedark/startthedark/templates/events/archive.html 中,第 32 行出错

无效的块标签:“else”,预期为“empty”或“endfor”

22                  {% csrf_token %}
23                  <input type="hidden" name="event_id" value="{{event.id}}"/>
24                  {% if attending %}
25                  <input class="attendance unattend" type = "submit" value = "Unattend" />
26                  {% else %}
27                  <input class = "attendance attend" type ="submit" value = "Attend"/>
28                  {% endif %}
29              </form>
30              -->
31          {% endfor %}        
32      {% else %}
33          <p>No events for today.</p>
34      {% endif %}
35  
36  {% endblock %}
37  

存档.html

{% extends "base.html" %}
{% load events_tags %}   
{% block title %}Archive -{{ block.super}}{% endblock %}
{% block main_content %}
    <a href="{% url ev_create %}">Create an Event</a>
    {% if events %}
        {% for e in events %}
            {% event e %}
                {% endfor %}        
    {% else %}
        <p>No events for today.</p>
    {% endif %}

{% endblock %}

events_tags.py

from django import template
from events.models import Attendance
def event(context, e):
    to_return = {
    'event' : e,
    #'request': context['request'],
    }
    if context['user'].is_authenticated():
        try:
            Attendance.objects.get(event=e,user = context['user'])#request.user)
            attending = True
        except Attendance.DoesNotExist:
            attending = False
        to_return.update({
            'attending':attending,
            'authenticated':True,
        })
    else:
        to_return['authenticated'] = False
    return to_return

register = template.Library()

register.inclusion_tag('events/event.html',takes_context=True)(event)
4

1 回答 1

4

The comment tag i.e. <!-- Comments --> is HTML comment. Django doesn't recognize them. If you have included template tags inside the comment block, Django process them instead of ignoring.

For multiline comments in Django use:

{% comment %}
 ......
 ........
{% endcomment %}
于 2013-06-14T01:51:23.357 回答