简体   繁体   中英

Create a 'dummy' URL path for frontend pictures rendering

I have application with DjangoRestFramework for backend and Vue.js for frontend. My user can upload pictures and they are stored in an uploaded_media file.

Therefore I added + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to my urlpatterns.

But since it is a Single Page Application, I added re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point"), in my urlpatterns for everything to be redirected to my index.html .

My issue is since my pictures URL is fetched in this format: "http://127.0.0.1:8000/media/my_pic.jpg" , it can't be rendered frontend because it's redirected to my entry-point .

So I made a 'dummy' URL path ( path('media/', DummyView.as_view()) ) pointing to a dummy View:

class DummyView(TemplateView):
    pass

And it works... just for the first picture. I am doing it right and missing something or I am going to the wrong direction?

If it helps, I find a better way than creating a 'dummy' view, I just change my entry-point URL this way:

re_path(r"^(?!media).*$", IndexTemplateView.as_view(), name="entry-point")

So everything except the URL starting with 'media' is redirected to the entry-point

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