0

I've always read that when you code in Django, you should follow the DRY principle and only hard-code your URLs in one place, usually the urls.py file. But how to you write the URL in a Django template if you're passing it to a jQuery Ajax function? Every online article I've read about using Ajax in Django templates shows the URL as hard-coded.

In my case, the template uses Bootstrap to display a modal window when the user pushes a button. My profile.html template contains the following jQuery code:

<html>
    # ...
    <script>
      $(function() {
        var url = '/profile/view/member-lists/{{ viewer_id }}/{{ viewed_id }}/{{ viewed_type_id }}';
        $('#exampleModal').on('show.bs.modal', function(e) {
          $.ajax(url, {
            url: url,
            type: 'GET',
            dataType: 'json',
            success: function(data) {
              // Do something
            },
            error: function(data) {
              // Raise error
            },
          });
        });
      });
    </script>
</html>

I've tried the following various ways of expressing the URL but none of them have worked:

  var url = "{% url 'member-lists' {{ viewer_id }} {{ viewer_type_id }} %}";
  var url = "{% url 'member-lists' viewer_id={{ viewer_id }} viewer_type_id={{ viewer_type_id }} %}";
  var url = "{% url 'member-lists' %}?viewer_id={{ viewer_id }}&viewer_type_id={{ viewer_type_id }}";

What is the correct way to express a URL in a Django template when that URL is passed to a jQuery Ajax function?

UPDATE

I changed my JavaScript url to this:

    var url = "{% url 'member-lists' viewer_id=viewer_id viewed_id=viewed_id viewed_type_id=viewed_type_id %}";

My urls.py file contains these to urlconf patterns:

url(r'^view/member-lists/(?P<viewer_id>\d+)/(?P<viewed_type_id>\d+)/$', get_viewer_member_lists, name='member-lists'),
url(r'^view/member-lists/(?P<viewer_id>\d+)/(?P<viewed_id>\d+)/(?P<viewed_type_id>\d+)/$', get_viewer_member_lists_checked, name='member-lists-checked'),

I'm entering this URL:

http://localhost:8000/profile/view/1/5/1/

But now I'm getting the error shown below. I would think that my URL would be matched by the second url pattern shown above.

NoReverseMatch at /profile/view/1/5/1/

Reverse for 'member-lists' with arguments '()' and keyword arguments '{u'viewed_id': u'5', u'viewed_type_id': u'1', u'viewer_id': u'1'}' not found. 1 pattern(s) tried: ['profile/view/member-lists/(?P\d+)/(?P\d+)/$']

Is there something I'm not understanding about Django's url pattern matching?

SOLUTION

I figured out the problem. The url pattern name needed to be "member-lists-checked" instead of "member-lists".

1 Answer 1

1

Just call it like this:

var url = "{% url 'member-lists' viewer_id=viewer_id  viewer_type_id=viewer_type_id viewed_type_id=viewed_type_id %}";

No need to use the double curly brackets.

BTW, note that you are not passing viewed_type_id, I don't know if this is needed in your urls.py. If it doesn't work please post your urls.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.