Modelesq
Modelesq

Reputation: 5402

Pass a form from a custom template tag

I'm trying to check whether a user is attending an event within a list of events. If They are, cool: say that they've 'Registered'. If not, throw a signup form into the template.

from django.core.context_processors import csrf

@register.simple_tag(takes_context=True)
def user_is_attending(context, event):
    request = context['request']
    profile = Profile.objects.get(user=request.user)
    attendees = [a.profile for a in Attendee.objects.filter(event=event)]
    if profile in attendees:
        return "<a href='#' class='button'>Thanks for Registering!</a>"
    else:
        return "<form method='POST' action='/event/{{ event.id }}/register/'><input type='hidden' name='csrfmiddlewaretoken' value='{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}' ><input type='hidden' name='username' value='{{ request.user.username }}' /><button class='btn' type='submit'><div class='timeleft'>{{ event.date|timeuntil|split_timeuntil|safe }} left</div><div class='register-text'>Register<br/><span>for this Event</span></div></button></form>"
     # I apologize for the lengthy form

The template tag works(it correctly checks). However, it returns:

and

So what are my options? Can I pass a form from a custom template tag? I don't really have use for a django form in this case, as it's literally just a button.

Thanks in advance for your input.

Upvotes: 0

Views: 94

Answers (2)

schacki
schacki

Reputation: 9533

Are you sure you need a tag? Try put the logic into your template like that (untested)

{if profile in attendees}
    <a href='#' class='button'>Thanks for Registering!</a>
{else}
    <form> ...></form>
{endif}

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599926

A simple tag is not parsed as a template. You should use an inclusion tag, and put both the HTML (plus the if/else logic) in the separate template.

Upvotes: 2

Related Questions