Django的。在引号内引用url标签 [英] Django. Use url tags inside quotes, inside quotes
问题描述
notificacion_string =<一个href = \{%url \'perfiles:perfil\'actor.usuario.username \'recientes\'%} \>%s< / a>投票在你的帖子% (notificacion.actor.usuario.username)
正如你所看到的,我已经尝试转义引号href =。但是我收到这个错误:
%u格式:需要一个数字,而不是unicode
所以,我估计当%url ..%()被评估时会发生错误。
额外的信息:
urlpefiles:perfil收到两个参数:
url(r'^(?P< username> \w +)/(?P< queryset& w +)/ $',views.perfil,name ='perfil'),
用户名和queryset,第一个来自notificacion.actor.usuario.username,第二个是一个字符串'recientes'。
提前感谢您的帮助
生成视图中的URL
from django.core.urlresolvers import reverse
#使用Django urlresolver反向生成URL
url = reverse('perfiles:perfil',kwargs = {'username': actor.usuario.username,'queryset':'recientes'})
#然后连接URL作为其他字符串参数
notificacion_string =< a href ='%s'> %s< / a>投票哟你的帖子%(url,notificacion.actor.usuario.username)
生成模板中的URL (HTML)
< a href ={%url'perfiles:perfil'actor.usuario.username'recientes'% }> {{notificacion.actor.usuario.username}}< / a>投票你的帖子
在jQuery中生成URL
< script>
//生成带有伪用户名的URL
var url ={%url'perfiles:perfil''FakeUsername''recientes '%}
//我们假设我们收到一个变量用户名的$ AJAX响应
username = response ['username']
//替换字符串'FakeUsername'为一个
url = url.replace('FakeUsername',username)
< / script>
正式的Django文档解释很好,所以建议您查看 Django URLresolvers
I want to pass this variable to the context and render it, it includes html tags.
notificacion_string = "<a href = \"{% url \'perfiles:perfil\' actor.usuario.username \'recientes\' %}\" > %s </a> voted on your post" % (notificacion.actor.usuario.username)
As you can see, I have tried escaping the quotes inside the href=" ". But I get this error:
%u format: a number is required, not unicode
So, I guess the error happens when "% url .." %() is evaluated. I have tried many things without success.
Extra information:
The url "pefiles:perfil" recieves two arguments:
url(r'^(?P<username>\w+)/(?P<queryset>\w+)/$',views.perfil, name='perfil'),
the arguments are a username and a queryset, the first one comes from notificacion.actor.usuario.username and the second one is a string, 'recientes'.
Thanks in advance for your help.
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
这篇关于Django的。在引号内引用url标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!