Django converts old url to new syntax

I'm trying to use this app in my project: https://github.com/s1n4/django-favorite but it has the old url syntax and I didn't figure out how to convert it to the new url syntax. The url I want to convert:

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

      

Its exact form in html:

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

      

I know the syntax has changed since Django 1.5 and I tried using this version:

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

      

It didn't work either. It says:

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

      

How can I fix this? Thank.

Edit: His template tag might help you understand:

@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})

      

+3


source to share


2 answers


Since this application is used in production (in which we used Django 1.4), I cannot fix the application to be compatible with Django 1.5+. But there is a pull request that I left open for a situation like this. Here it is: https://github.com/s1n4/django-favorite/pull/1 This might solve the problem.



+2


source


The url has no arguments:

url(r'^add-or-remove$', 'add_or_remove'),

      

and that doesn't look like either:

def add_or_remove(request):

      

The URL tag in HTML also has no arguments:



{% url favorite.views.add_or_remove %}

      

To convert this to the new syntax, it would be:

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

      

After that, you can use the template tag as described in the README:

{% load favorite_tags %}

{% for comment in post.comments %}
  {% favorite_button comment %}
{% endfor %}

      

+2


source







All Articles