我的Django模板使用if怎么了? [英] What is wrong with my Django templating use of if's?

查看:88
本文介绍了我的Django模板使用if怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $%$ $ $ $ $ $ $ $ $ $ $ 右键单击编辑。
href ={%if:不在url.url%} http:// {%endif%} {{url.url}}> {{url.url}}< / a> ;
< span class =deleteid =URL _ {{url.id}}>& amp; amp;#10008;< / span> &安培; NBSP;
{%endfor%}

启发式用于预先添加部分或完整的网址,如google.com,假设有时人们会粘贴完整的浏览器网址,有时候人们会输入google.com,从不输入'​​http://'。



模板引擎抱怨{%if:not in url.url%}是无效的语法。这里有什么正确的语法/方法?

解决方案

如何为此使用过滤器:

  href ={{ulr.url | urlize}}

请记住在此之前检查以建立自己的(查找urlize):
https://docs.djangoproject.com/en/dev/ref/templates/builtins/






我认为一个更好的方法是将URL保存为管理员中的绝对URL,并在显示链接时显示 http:// ...


{% for url in urls %}
    <a id="URL_url_{{ url.id }}" class="edit_rightclick"
      title="RIGHT click to edit."
      href="{% if ":" not in url.url %}http://{% endif %}{{ url.url }}">{{ url.url }}</a>
    <span class="delete" id="URL_{{ url.id }}">&amp;#10008;</span> &nbsp;
{% endfor %}

The heuristic is intended to prepend the value of a partial or complete URL like google.com, under the assumption that sometimes people will paste a full browser URL, and sometimes people will type out google.com and never type 'http://'.

The templating engine is complaining that '{% if ":" not in url.url %}' is invalid syntax. What is the correct syntax / approach here?

解决方案

What about using a filter for this:

href="{{ ulr.url|urlize }}"

Remember to check here before to build your own (look for urlize): https://docs.djangoproject.com/en/dev/ref/templates/builtins/


I think a better approach would be to the save the URLs as absolute ones within the admin and strip "http://" when showing the link...

这篇关于我的Django模板使用if怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆