Skip to content

Commit 61bce55

Browse files
committed
fix: handle empty urls when append_slash=True
1 parent 9c6027a commit 61bce55

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

demo/demo/urls_with_slash.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Same as urls.py, but with `append_slash=True`"""
2+
3+
from django.contrib import admin
4+
from django.urls import path
5+
6+
from file_router import file_patterns
7+
8+
urlpatterns = [
9+
path("admin/", admin.site.urls),
10+
*file_patterns("demo/views", append_slash=True),
11+
]

demo/demo/views_test.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ def color():
99
return Color.objects.create(name="Foo Color", slug="foo", code="00ff00")
1010

1111

12+
def test_not_a_view(client):
13+
with pytest.raises(NoReverseMatch):
14+
reverse("not_a_view")
15+
16+
response = client.get("/not-a-view")
17+
assert response.status_code == 404
18+
19+
20+
def test_append_slash(settings):
21+
settings.ROOT_URLCONF = "demo.urls_with_slash"
22+
assert reverse("home") == "/"
23+
assert reverse("colors") == "/colors/"
24+
assert reverse("colors_add") == "/colors/add/"
25+
assert reverse("colors_slug", args=["abc"]) == "/colors/abc/"
26+
27+
1228
def test_home(client):
1329
url = reverse("home")
1430
assert (
@@ -29,14 +45,6 @@ def test_current_time(client):
2945
assert response.status_code == 200
3046

3147

32-
def test_not_a_view(client):
33-
with pytest.raises(NoReverseMatch):
34-
reverse("not_a_view")
35-
36-
response = client.get("/not-a-view")
37-
assert response.status_code == 404
38-
39-
4048
@pytest.mark.django_db
4149
def test_colors(client, color):
4250
url = reverse("colors")

file_router/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def file_patterns(start_dir, append_slash=False):
4747
except AttributeError:
4848
url = "" if file == "__init__.py" else file.replace(".py", "")
4949
url = start_dir_re.sub("", f"{root}/{url}").strip("/")
50-
url = (url + "/") if append_slash else url
50+
url = (url + "/") if append_slash and url != "" else url
5151

5252
try:
5353
urlname = view_fn.urlname

0 commit comments

Comments
 (0)