0

This used to work before Django 2.0 changed url patterns from "url" to "path":

index.html

<!DOCTYPE html>
  {% load static %}
  <head>
      <script type="text/javascript" src="{% static 'main/js/jquery-3.3.1.js' %}">
  </head>
  <body>
      <div id='test'>
        <p><button class="btn">Click Here!</button></p>
      </div>
  <script>
      $('.btn').click(function(){
      console.log('button is clicked!')
      $.ajax({
          url: 'main/all_json',
          sucess: function(serverResponse){
          console.log('success.serverResponse', serverResponse)
    }
  })  
});

APP LEVEL urls.py

urlpatterns = [
  url(r'^all_json$',views.all_json, name="all_json")
  ]

Project Level urls.py

app_name= "main"

urlpatterns = [
    path('', include ('apps.main.urls', namespace='main')),
    path('admin/', admin.site.urls),
]

views.py

def all_json(request):
    return HttpResponse ('hello world!')

But now, Django 2.0 uses "path" instead of the url regex pattern. When I use path:

app_name= "name"

urlpatterns = [
path('all_json',views.all_json, name="all_json"),
]

I get the:

GET http://127.0.0.1:8000/main/all_json 404 (Not Found)

I looked in the new documentation and release notes and there are some SO answers that explain how to use it SO post 1 & SO post 2. That has been useful up to this point, where I'm unable to pass the url from the AJAX function to the "path".

I'm new to AJAX and I'm used to using the {% url main:all_json %} in Django for my actions. But with AJAX I believe I can't use this notation. Is that right?

And for some reason, the examples that I have that used url(r'^$') urlpatterns before Django 2.0 worked, but now I get a code 404 when using 'path'. Most of the questions and tutorials available are pre Django 2.0 and use url(r'^$') urlpatterns. Release notes and documentation do not mention anything about differences in working with AJAX.

My questions is the following:

Is there something else that I need to add in my template and/or urls.py to help it find the urls(get rid of the 404)?

L. P.
  • 165
  • 1
  • 19

2 Answers2

3

First, url is still perfectly valid in Django 2.0. In later versions exactly the same functionality is available as re_path.

However, the problem is not there. It is that you have added a final slash in the new version where you didn't have one before. Remove it:

path('all_json', ...)

or, preferably, add it to the Ajax call:

url: 'main/all_json/',

Note finally that since the Ajax script is directly in the template file, it's absolutely possible to use the {% url %} tag there.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you for your reply. My apologies. I should have mentioned that I experimented with leading and trailing "/" and that did not work. I'm still getting the 404. – L. P. Feb 23 '19 at 22:10
  • As I say, you can use the url tag. But otherwise, please show your project level urls.py. – Daniel Roseman Feb 23 '19 at 22:16
  • Daniel, I added the project level urls.py. I tried to use re_path, but it seems that it doesn't like it when I use both path and re_path in the same file. For what I see in the documentation it's one or the other. If I want to use re_path I have to convert all the other routes into url(r'^$') format. – L. P. Feb 23 '19 at 22:23
  • 1
    Hmm you don't seem to have "main" as part of any URL pattern. Why are you using it in the Ajax call? The namespace isn't part of the URL itself, and this hasn't changed with the new path syntax. – Daniel Roseman Feb 23 '19 at 22:24
  • 1
    And no, you can mix and match path and re_path. – Daniel Roseman Feb 23 '19 at 22:24
  • Thank you for clarifying the url and namespace bit. My mistake was adding "main/". I didn't need it. – L. P. Feb 23 '19 at 22:47
1

Try to build API's that clear and useful. Using main, all_json names are unclear. Nevertheless, let's try on your examples:

  1. In your urls.py use main/all_json/ and name="all_json". Accoding to documentation:

There’s no need to add a leading slash, because every URL has that. For example, it’s articles, not /articles. link

...each pattern requires that the URL end with a slash. link

  1. In your HTML template (by the way, it's maybe mistake, but you named it html.py. I advise to refactor this to somename.html), in the js block use template tag {% url "all_json"" %} like:

    $.ajax({
          url: '{% url "all_json" %}',
          sucess: function(serverResponse){
          console.log('success.serverResponse', serverResponse)
    }
    

    })

By using url template tag you can avoid many mistakes when changing urls.

Community
  • 1
  • 1
fanni
  • 1,149
  • 8
  • 11