Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从自定义模板标记传递表单_Python_Django_Django Templates - Fatal编程技术网

Python 从自定义模板标记传递表单

Python 从自定义模板标记传递表单,python,django,django-templates,Python,Django,Django Templates,我试图检查用户是否正在参加事件列表中的事件。如果他们是,酷:说他们已经“注册”。如果没有,则向模板中抛出一个注册表单 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

我试图检查用户是否正在参加事件列表中的事件。如果他们是,酷:说他们已经“注册”。如果没有,则向模板中抛出一个注册表单

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
处理器从django.core.context\u导入csrf @register.simple\u标记(接受\u context=True) def用户正在参加(上下文、事件): 请求=上下文['request'] profile=profile.objects.get(用户=request.user) attendees=[a.profile for a in Attendee.objects.filter(event=event)] 如果与会者中有个人资料: 返回“” 其他: 为此事件返回“{event.date | timeuntil | split | timeuntil | safe}}}leftRegister
” #我为冗长的表格道歉 模板标记起作用(它正确地检查)。然而,它返回:

  • event.date | timeuntil | split | timeuntil | safe left |将此事件注册为字符串
    应为:“还剩4天|注册此事件”

  • 我得到一个
    CSRF验证失败。请求中止
    错误。因为它没有正确通过
那么我有什么选择呢?我可以从自定义模板标记传递表单吗?在本例中,我并不真正使用django表单,因为它实际上只是一个按钮


提前感谢您的输入。

简单的标记不会被解析为模板。您应该使用一个包含标记,并将两个HTML(加上if/else逻辑)放在单独的模板中。

您确定需要标记吗?尝试像这样将逻辑放入模板中(未测试)

{if profile in attenders}
{else}
...>
{endif}

attenders=[a.Attendee.objects.filter(event=event)]中的a.profile文件
这是浪费。如果Attendee.objects.filter(event=event,a\u profile=profile.count():
{if profile in attendees}
    <a href='#' class='button'>Thanks for Registering!</a>
{else}
    <form> ...></form>
{endif}