James Franco
James Franco

Reputation: 4706

Django urls py. Loading the index page instead of the image

I currently have two situations

A) http://127.0.0.1:8000/ B) http://127.0.0.1:8000/Images/SomeImages.png

This is what my urls.py looks like

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'',       include("webSite.urls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and then my webSite.urls looks like this

urlpatterns = [
    url(r"", test , name="test"),
]

The problem with this is it works for condition A but for condition B it routes to the main page as well instead of the image. Can anyone tell me how I can fix this?

Upvotes: 1

Views: 25

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

You should include the ^ and $ anchors to mark the start and end of the string:

urlpatterns = [
    url(r'^$', test , name="test"),
]

or work with a path(…) which will compile a regex with these anchors:

urlpatterns = [
    path('', test , name="test"),
]

Upvotes: 1

Related Questions