0

When jsevent triggered , it forwards link parameter to data-url attribute of a button and when user click that button it redirects to django backend view but there is a problem with parsing line of JS
$('#btn_del').attr('data-url',{% url 'event_delete' + event.id + %}); is there any chance to combine that syntax ? Thank you , have a nice days.

JS:

  eventClick: function(event, jsEvent, view) {
          $('#modalTitle').html(event.id);
          $('#btn_del').attr('data-url',`{% url 'event_delete' ` + event.id + `%}`);
          $('#calendarEditModal').modal();
          console.log(event.id);
        }

url.py

....
url(r'^event/(?P<pk>\d+)/delete/$', event_delete, name='event_delete'),
....

Html button that redirects to django

<button id="btn_del" type="button" class="btn btn-primary js-delete-events" data-url="">
          <span class="glyphicon glyphicon-pencil"></span>
          Edit
        </button>

Result should be like this format event.pk is going to be our parameter numeric value , it's ok.

    <button type="button" class="btn btn-primary js-delete-events" data-url="{% url 'event_delete' event.pk %}">
      <span class="glyphicon glyphicon-plus"></span>
      button
    </button>

/event/1/update/  is the last result of seen from browser inspect. But I need to write inside jsEvent with django syntax to able to reach backend view which is {% url 'event_delete' event.pk %} something like that.
BCA
  • 438
  • 6
  • 21
  • Please show the final HTML, not server-side source. –  May 13 '19 at 14:41
  • @ChrisG that's the opposite of what is needed. "Could not parse the remainder" is an error from the Django template language. – Daniel Roseman May 13 '19 at 15:02
  • @BCA I really can't understand how you expect this to work. Django template tags are evaluated on the server, long before the JS executes in the browser. – Daniel Roseman May 13 '19 at 15:03

1 Answers1

1

No, it cannot be done that way... Parsing of your templates (the {% url %} tag) is done on the server before even the browser receives anything. Browser doesn't know what template tags are you using.

On the other side, JavaScript is executed in the browser, so template language has also no knowledge about variables inside it.

Solution for that is to use a package like Django JS Reverse. The second one is to pass full URL to the JavaScript, just like it receives the ID of the item.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • Ok , I understand. thank you. but what if I send to button attribute how should I call that specific view function ? I send that link like this: I need to trigger that view function. Im new at django python sorry if its not logical. – BCA May 14 '19 at 06:17
  • Just use it in your javascript now. Sorry, this is not Django-related question anymore, but JavaScript related and I don't have the knowledge to help you... – GwynBleidD May 14 '19 at 09:29