图像未显示Django + Angular

时间:2016-12-13 19:55:34

标签: angularjs django django-rest-framework

早些时候,当我刷新页面时,我遇到了问题。有了这个Solution,我设法解决了这个问题。但在将此应用于url模式后,图像无法正确加载。如果我尝试在新标签中打开图像源,它会将我重定向到索引页面。

当网址格式为url(r'^.*$', IndexView.as_view(), name='index'),时,不会显示图片,但会正确刷新网页。

当网址格式为url(r'^$', IndexView.as_view(), name='index'),时显示图片但页面未正确刷新(找不到网页)错误

如何解决这个问题。

更新: urls.py

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'),
url(r'^api/v1/auth/logout/$', LogoutView.as_view(), name='logout'),
url(r'^api/v1/', include(accounts_router.urls)),
url(r'^api/v1/', include(profiles_router.urls)),
url(r'^blogs/',include('blogs.urls')),
url(r'^account_data/',include('customauth.urls')),
url(r'^.*$', IndexView.as_view(), name='index'),
#url(r'^customauth/',include('customauth.urls')),
]
if settings.DEBUG:
    urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

1 个答案:

答案 0 :(得分:0)

在django中,url是从列表的第0个索引中解析的,因此.*的优先级高于/static//media/,因此更改网址的顺序为static& media的优先级高于IndexView

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'),
url(r'^api/v1/auth/logout/$', LogoutView.as_view(), name='logout'),
url(r'^api/v1/', include(accounts_router.urls)),
url(r'^api/v1/', include(profiles_router.urls)),
url(r'^blogs/',include('blogs.urls')),
url(r'^account_data/',include('customauth.urls')),
#url(r'^customauth/',include('customauth.urls')), rest of the urls
]
if settings.DEBUG:          
    # static & media urls
    pass  
urlpatterns+= [url(r'^.*$', IndexView.as_view(), name='index'),] # accepts any urls otherthan above