曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Django

Configuring Django Static File Routing After Disabling Debug

When Django's Debug mode is turned off, requesting static files returns a 404 response code. The backend request URL is "GET /static/css/404.css HTTP/1.1" 404 1217, and the static file URL cannot be found. Static files can be obtained by configuring the static file URL and starting from the command line. URL Configuration Django static files are managed through ``. Serving static file responses is implemented via the serve() function. The configuration in url.py is as follows: fr

曦远 (DealiAxy)约 4 分钟
目录3 个章节

When Django's Debug mode is turned off, requesting static files returns a 404 response code. The backend request URL is "GET /static/css/404.css HTTP/1.1" 404 1217, and the static file URL cannot be found. Static files can be obtained by configuring the static file URL and starting from the command line.

URL Configuration

Django static files are managed through ``. Serving static file responses is implemented via the serve() function. The configuration in url.py is as follows:

from django.conf.urls import url
from django.contrib import admin
from django.contrib.staticfiles import views as view

from blog import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'home/', views.Test.as_view(), name='test'),
    # 静态文件的url配置
    url(r'^(?P<path>.*)$', view.serve),
]

Static Files

The static file configuration remains unchanged.

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'statics'),
    os.path.join(BASE_DIR, 'bootstrap-3.3.7'),
]

Startup

Do not start via PyCharm's shortcut icon; if you start that way, the files still cannot be accessed. Start with the command:

python manage.py runserver 127.0.0.1:80 --insecure

to start, where --insecure forces Django to handle static files.

After this configuration, the static file URLs can match the corresponding static files.

_EOF_

Configuring Django Static File Routing After Disabling Debug

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

聊聊Django应用的部署和性能的那些事儿

前言 随着工作的深入,我越来越发现Python Web开发中有很多坑,也一直在羡慕AspNetCore和Go等的可执行文件部署和高性能,以及Spring生态的丰富,不过因为工作用了Django,生活还是要继续的嘛,这Django好歹也是有很大份额的Web框架,也没那么不堪,至少开发速度上就吊打一众框架了~ 在之前的文章里我介绍过使用Docker部署Django应用的方法,不过那种部署方式只适合上线

2022-05-27 · Django · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…