0

I have a Django 2 + Angular 6 project. I have a build process that moves my compiled JS files into the Django's static folder.

However, whenever I go to a link, say http://127.0.0.1:8000/#/upload, the browser always redirect me to http://127.0.0.1:8000/static/my_project/#/upload.. What is happening?

I'm using hash location strategy. My page still works, but whenever I refresh at the latter URL or visit it directly, I get an 404..

My urls.py file is like this:

urlpatterns = [
    path('', IndexView.as_view()),
    path('admin/', admin.site.urls),
    path('api/v1/', include(router.urls)),
]

The first url pattern matches an IndexView, which is just a TemplateView with this index.html:

{% load static from staticfiles %}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="icon" type="image/x-icon" href="{% static 'favicon.ico' %}">
</head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="{% static 'uploader/runtime.js' %}"></script>
<script type="text/javascript" src="{% static 'uploader/polyfills.js' %}"></script>
<script type="text/javascript" src="{% static 'uploader/styles.js' %}"></script>
<script type="text/javascript" src="{% static 'uploader/vendor.js' %}"></script>
<script type="text/javascript" src="{% static 'uploader/main.js' %}"></script>
</body>
</html>
7ball
  • 2,183
  • 4
  • 26
  • 61
  • Could the cause be on the angular side? From the symptoms, it seems to me like your angular application changes the url to the latter without reloading the page. – Ozgur Akcali Nov 02 '18 at 09:42
  • You can set base url setting,but your deploy method is not recommended – Hayden Nov 02 '18 at 10:06
  • can you share your settings.py file data looks like can be an issue in URL their. – somsgod Nov 02 '18 at 10:23

1 Answers1

1

Ok so I figured it out... Posting for future readers. If you are 100% certain that you're using hash location strategy and everything worked, then make sure you don't have a

<base href="/static/..."> in your <head> tags (I removed it in my original question for confidentiality purposes.. should have probably obscured it instead of removing it altogether...

The above happened because I used to following command to build:

ng build --base-href /static/...

7ball
  • 2,183
  • 4
  • 26
  • 61