Django. Use url tags inside quotes, inside quotes

陌路散爱 提交于 2019-12-03 21:04:47

Generate URL in view

from django.core.urlresolvers import reverse

# Generate the URL using the Django urlresolver reverse
url = reverse('perfiles:perfil', kwargs={'username': actor.usuario.username, 'queryset': 'recientes' })

# Then concatenate the URL as other string argument
notificacion_string = "<a href ='%s'> %s </a> voted on your post" % (url, notificacion.actor.usuario.username)

Check Django URLresolvers

Generate URL in template (HTML)

<a href = "{% url 'perfiles:perfil' actor.usuario.username  'recientes' %}" >{{notificacion.actor.usuario.username}}</a> voted on your post"

Generate URL in jQuery

<script>
    // Generate an URL with a fake username
    var url = "{% url 'perfiles:perfil' 'FakeUsername'  'recientes' %}"

    // Let's supose we received an AJAX response with a variable username
    username = response['username']

    // Replace the string 'FakeUsername' for the real one
    url = url.replace('FakeUsername', username)
</script>

The official Django documentation is quite well-explained so I recommend you to check Django URLresolvers

There's no need to escape any of those quotes. The context for evaluating the code inside the template tag is completely separate from the surrounding HTML, so they do not interfere.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!