David Torrey
David Torrey

Reputation: 1352

Django serving static html file with javascript

I have an odd case where I'm trying to serve a static index.html file that holds a javascript bundle from webpack in it for vue.js files, but it is sending the incorrect javascript file contents.

I've tried both sending the html file as a template view and simply reading the file contents and sending it as an html response. It sends the html file itself correctly, but instead of sending the contents of the javascript file it just copies the index.html content into the javascript file.

I checked to make sure that I wasn't accidentally overwriting the javascript file with html content and that's not the case, the javascript is in correct form, but when it is sent to the browser this is what is seen:

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>testprojsimple</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="js/myvuejs.bundle.js"></script>
  </body>`
</html>

myvuejs.bundle.js:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>testprojsimple</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="js/myvuejs.bundle.js"></script>
  </body>`
</html>

Any ideas on why it would be re-sending the index.html content as the javascript file as well?

Edit:

I'm in development mode currently, serving by simply saying in views:

def indexView(request):
    return HttpResponse(open("file/to/static/html/file/index.html").read())

Settings.py:

STATIC_ROOT = os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

urls.py:

urlpatterns = [
    url(r'^', indexView),
]

My goal is to avoid serverside rendering, using client-side frameworks to reach into a django backend that serves data content, like json, etc...

Upvotes: 0

Views: 973

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600049

The regex ^ matches every single URL; it just means "any string that starts", which of course is every string. You should terminate the pattern as well: ^$, so that it only captures the empty string.

Upvotes: 1

Related Questions