Django.urls reversing urls through multiple url pattern lists within one app
Question:
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
)
Answers:
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.
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
)
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.