Calling Django `reverse` in client-side Javascript

前端 未结 10 489
无人共我
无人共我 2020-12-13 02:22

I\'m using Django on Appengine. I\'m using the django reverse() function everywhere, keeping everything as DRY as possible.

However, I\'m having trouble

相关标签:
10条回答
  • 2020-12-13 02:55

    I like Anatoly's idea, but I think using a specific integer is dangerous. I typically want to specify an say an object id, which are always required to be positive, so I just use negative integers as placeholders. This means adding -? to the the url definition, like so:

    url(r'^events/(?P<event_id>-?\d+)/$', events.views.event_details),
    

    Then I can get the reverse url in a template by writing

    {% url 'events.views.event_details' event_id=-1 %}
    

    And use replace in javascript to replace the placeholder -1, so that in the template I would write something like

    <script type="text/javascript">
    var actual_event_id = 123;
    var url = "{% url 'events.views.event_details' event_id=-1 %}".replace('-1', actual_event_id);
    </script>
    

    This easily extends to multiple arguments too, and the mapping for a particular argument is visible directly in the template.

    0 讨论(0)
  • 2020-12-13 02:56

    There is another method, which doesn't require exposing the entire url structure or ajax requests for resolving each url. While it's not really beautiful, it beats the others with simplicity:

    var url = '{% url blog_view_post 999 %}'.replace (999, post_id);
    

    (blog_view_post urls must not contain the magic 999 number themselves of course.)

    0 讨论(0)
  • 2020-12-13 02:56

    One of the solutions I came with is to generate urls on backend and pass them to browser somehow.

    It may not be suitable in every case, but I have a table (populated with AJAX) and clicking on a row should take the user to the single entry from this table.

    (I am using django-restframework and Datatables).

    Each entry from AJAX has the url attached:

    class MyObjectSerializer(serializers.ModelSerializer):
        url = SerializerMethodField()
        # other elements
    
        def get_url(self, obj):
           return reverse("get_my_object", args=(obj.id,))
    

    on loading ajax each url is attached as data attribute to row:

    var table = $('#my-table').DataTable({
       createdRow: function ( row, data, index ) {
          $(row).data("url", data["url"])
       }
    });
    

    and on click we use this data attribute for url:

    table.on( 'click', 'tbody tr', function () {
      window.location.href = $(this).data("url");
    } );
    
    0 讨论(0)
  • 2020-12-13 02:58

    Use this package: https://github.com/ierror/django-js-reverse

    You'll have an object in your JS with all the urls defined in django. It's the best approach I found so far.

    The only thing you need to do is add the generated js in the head of your base template and run a management command to update the generated js everytime you add a url

    0 讨论(0)
提交回复
热议问题