gpt4 book ai didi

python - Django 将旧网址转换为新语法

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:44 26 4
gpt4 key购买 nike

我正在尝试在我的项目中使用此应用:https://github.com/s1n4/django-favorite但它有旧的 url 语法,我不明白如何将它转换为新的 url 语法。我要转换的网址:

{% url favorite.views.add_or_remove target_model target_object_id %}

它在 html 中的确切形式:

<button class="btn favorite" href="{% url 'favorite.views.add_or_remove' %}" model="{{ target_model }}" id="target_{{ target_object_id }}">

我知道 Django 1.5 的语法发生了变化,我尝试使用这个版本:

{% url 'favorite.views.add_or_remove' target_model target_object_id %}

它也没有用。它说:

Reverse for 'favorite.views.add_or_remove' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我该如何解决这个问题?谢谢。

编辑:它的模板标签可以帮助理解:

@register.simple_tag(takes_context=True)
def favorite_button(context, target):
user = context['request'].user

# do nothing when user isn't authenticated
if not user.is_authenticated():
return ''

target_model = '.'.join((target._meta.app_label, target._meta.object_name))
target_content_type = ContentType.objects.get_for_model(target)
target_object_id = target.id
fav_count = Favorite.objects.filter(target_content_type=target_content_type,
target_object_id=target_object_id).count()
undo = False
if user.favorite_set.filter(target_content_type=target_content_type,
target_object_id=target_object_id):
undo = True

return render_to_string('favorite/button.html',
{'target_model': target_model, 'target_object_id': target_object_id,
'fav_count': fav_count, 'undo': undo})

最佳答案

由于该应用程序正在生产中使用(我们在其中使用 Django 1.4),我无法修补该应用程序以使其与 Django 1.5+ 兼容。但是对于这种情况,我有一个拉取请求。这是:https://github.com/s1n4/django-favorite/pull/1它可能会解决问题。

关于python - Django 将旧网址转换为新语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25730440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com