Python中文
首页
教程
问答
标签
搜索
登录
注册
从自定义模板标记传递表单
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在尝试检查用户是否正在参加事件列表中的事件。如果他们是,酷:说他们已经注册了。如果没有,则在模板中抛出一个注册表单。你知道吗</p> <pre><code>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 </code></pre> <p>模板标签工作(它正确地检查)。但是,它返回:</p> <ul> <li><code>event.date|timeuntil|split_timeuntil|safe left | Register for this event</code>应为“还剩4天|注册此事件”</li> </ul> <p>以及</p> <ul> <li>我得到一个<code>CSRF verification failed. Request aborted</code>错误。因为它没有正确通过?你知道吗</li> </ul> <p>那我有什么选择呢?我可以从自定义模板标记传递表单吗?在本例中,我并不真正使用django表单,因为它实际上只是一个按钮。你知道吗</p> <p>提前感谢您的意见。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>简单标记不作为模板进行分析。您应该使用一个包含标记,并将两个HTML(加上if/else逻辑)放在单独的模板中。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
无法使用Django restfram生成PDF
9 回答
无法使用Django Rest框架发送压缩的gzip数据
2 回答
无法使用Django rest框架进行身份验证(请求用户=匿名用户)
3 回答
无法使用Django、Python和JavaScrip触发onclick函数
5 回答
无法使用Django.views.generic.View保存表单
3 回答
无法使用Django(python 2.7,OS X 10.11.1)
5 回答
无法使用Django/mongoengine连接到MongoDB(身份验证失败)
2 回答
无法使用Django\u mssql\u后端迁移到外部hos
10 回答
无法使用Django&Python3.4连接到MySql
2 回答
无法使用Django+nginx上载媒体文件
9 回答
无法使用Django1.6导入名称模式
1 回答
无法使用Django1.7和mongodb登录管理站点
3 回答
无法使用Djangoadmin创建项目,进程使用了错误的路径,因为我事先安装了错误的Python
4 回答
无法使用Djangockedi验证CBV中的字段
3 回答
无法使用Djangocketditor上载图像(错误400)
1 回答
无法使用Djangocron进行函数调用
8 回答
无法使用Djangofiler djang上载文件
10 回答
无法使用Djangokronos
6 回答
无法使用Djangomssql provid
5 回答
无法使用Djangomssql连接到带有Django 1.11的MS SQL Server 2016
10 回答