Last active
September 3, 2022 22:41
-
-
Save dsl400/2aacbb6fb8b9e6f4b51187050f04de0d to your computer and use it in GitHub Desktop.
django serve angular static
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ANGULAR_DIR_NAME= 'dist' | |
| ANGULAR_APP_DIR = os.path.join(BASE_DIR, f'../{ANGULAR_DIR_NAME}') | |
| STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') | |
| STATIC_URL = '/static/' | |
| STATICFILES_DIRS = [ | |
| ANGULAR_APP_DIR, | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from mySite.views import HomePageView | |
| from mySite.views import get_static | |
| urlpatterns = [ | |
| path('', HomePageView.as_view()), | |
| re_path('^(main|polyfills|runtime)\.[a-z0-9]{16}\.js$',get_static), | |
| re_path('^styles\.[a-z0-9]{16}\.css$',get_static), | |
| path("admin/", admin.site.urls), | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.http import HttpResponse | |
| from django.views import static | |
| from django.views.generic import TemplateView | |
| from django.http import HttpResponseNotFound | |
| from os import path | |
| from django.conf import settings | |
| class HomePageView(TemplateView): | |
| def get(self, request, **kwargs): | |
| return static.serve(request,'index.html', settings.ANGULAR_DIR_NAME) | |
| def get_static(request,match=None): | |
| try: | |
| file = open(path.join(settings.ANGULAR_APP_DIR,request.path[1:]),'rb') | |
| except: | |
| return HttpResponseNotFound() | |
| content_type = 'text/css' if match is None else 'text/javascript' | |
| return HttpResponse(file,content_type='text/javascript') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment