简体   繁体   中英

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?

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"),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM