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:
event.date|timeuntil|split_timeuntil|safe left | Register for this eventas a string when it should be: “4 days left| Register for this event”
and
- I get a
CSRF verification failed. Request abortederror. due to it not passing through correctly?
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.
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.