سؤال

I'm currently doing some starting stuff on Django 1.3.1 and following error struck me for 2 hours. Help me in figuring out the error. I have included my code on bitbucket.

Error:-

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

Template error

In template /home/virus/py_tut/startthedark/startthedark/templates/events/archive.html, error at line 32

Invalid block tag: 'else', expected 'empty' or '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  

Archive.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)
هل كانت مفيدة؟

المحلول

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 %}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top