Django - serving a SPA from static folder

Zak

Our django server is our API as well as an operations backend. I'm working on improving our ops backend by writing a Vue SPA that slowly replaces the existing ops backend.

I'm frontend and a little lost in intricacies of Django configs. Could someone suggest a sane solution for this problem?

I would like for the app to be served out of static folder, and have set:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, '../console/dist'),
)

This works and I can see my code when I view source, but only at http://localhost:8000/static/console/index.html

1) this won't work because as a SPA, Vue needs control of routing. From the Django side how can I have /static/console/* use my Vue app? On the Vue end, what do I have to configure in Webpack and Vue-router?

2) despite the fact that I can see my compiled app source, I get errors:

Creating Application Cache with manifest http://localhost:8000/static/appcache/manifest.appcache
index.html:1 Application Cache Checking event
index.html:1 Application Cache Downloading event
index.html:1 Application Cache Progress event (0 of 7) http://localhost:8000/favicon.ico
index.html:1 Application Cache Error event: Resource fetch failed (4) http://localhost:8000/favicon.ico
index.html:1 Uncaught (in promise) TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

which is weird because http://localhost:8000/favicon.ico is a working URL. I have a feeling this is also a Webpack problem.

What are my options?

Jan Giacomelli

I made it like this for Angular2 app with Django(it works without problems) - I think it can help you.

urls.py:

# catch-all pattern for compatibility with the Angular routes
url(r'^$', views.index),
url(r'^(?P<path>.*)/$', views.index),

views.py

def index(request, path=''):
    """
    Renders the Angular2 SPA
    """
    return render(request, 'index.html')

index.html

{% load staticfiles %}
...some other stuff...
<app-root>Loading... </app-root>
<script src="{% static 'js/jquery-3.1.1.min.js' %}"></script>
<script src="{% static  'js/materialize.min.js'%}"></script>

<!-- Angular app -->
<script type="text/javascript" src="{% static 'js/app/inline.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'js/app/vendor.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'js/app/main.bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'js/app/scripts.bundle.js' %}"></script>¸

settings.py

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

My index.html is stored in templates directory, my js app files are stored in static/js/app.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Django serving static files locally

NodeJS Express Angular serving a login page entirely separate from the SPA

NextJS - serving static content from .next

GitHub pages serving from docs and jekyll source folder config

PHP App Engine Flex serving SPA and API

Nextjs: Unable to load images from static folder

Serving an angular dist folder with go

Error serving static content Dropwziard

Serving static files with C++

Express js is not serving static files

Express: serving static files not working

Site not serving media files but serving static files correctly

Serving a SPA via Plug.Conn.send_file

How to run gunicorn from a folder that is not the django project folder

Cannot read Resource from /static folder in Spring Boot

Serving Static Files with a HTTP 500 Status

Django is not serving media files in debug environment

How to separate docker and docker-compose from Django source folder

Webpack-dev-server serving in different folder than webpack output

Django can load image from static files but not using css

Dropwizard Assets not serving static content outside of root path

Is http.StripPrefix necessary when serving static files in Go?

Changing default URL mapping for Serving Static Content in Spring Boot

Serving static files with spaces in file names with Spring Boot

Nginx slow static file serving (slower than node?)

Spring boot not serving static content when Jersey REST is included

Serving static HTML files in Nginx without extension in url

Dropwizard Assets not serving static content outside of root path

ASP.NET Core / Kestrel not serving static content consistently