0

I have a url.py inside my project which includes the following urls.py (belongs to my app).

urls.py

from django.urls import path,include
from .views import Index, Foo

bar_urlpatterns = [
path('foo/', Foo.as_view(), name='foo'),
]
        
urlpatterns = [
path('', Index.as_view(), name='index'),       
path('bar/', include(bar_urlpatterns)),]

I'm trying to outsource the subpaths of a path. The docs says the function include can include a pattern_list and when i call the url"http://myurl/foo/bar" directly, this seems to hold true. I can also load the view via ajax directly when i give it the string.

But when i try to use the reverse {%url 'foo'} url template tag this throws:

Uncaught SyntaxError: Invalid regular expression flags(at ...)

Doing the same thing with non-outsourced url patterns works perfectly fine for me.

The html elements where i use the function:

<a onclick="load_tab_view({% url "foo" %})">Foo</a>

<div id="tab_view_replaceable"></div>

js (works fine with my other views)

function load_tab_view(url){
    replace_id = 'tab_view_replaceable';
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'html',
            success: function(data){
                $('#'+replace_id).html(data);
            }
        });
    
}

Is there a way in which im still able to outsource my subpaths and make use of the reverse url template tag?

(I dont want to create a new app for bar)

1 Answer 1

1

You should be able to use it if you put it in a different kind of quotation marks. So alternate between the 3 types.

In your case it should work like:

<a onclick="load_tab_view(`{% url 'foo' %}`)">Foo</a>
<div id="tab_view_replaceable"></div>

That's because JS doesn't take it as string otherwise.

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.