不使用Django静态文件

huangapple go评论83阅读模式
英文:

Not use Django static files

问题

我对Django相当新手,希望我的问题不会太傻。我目前正在本地运行我的项目,每次在浏览器中进行API调用时,我都会看到服务器记录类似以下内容:

my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/css/default.css HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/bootstrap.min.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/jquery-3.5.1.min.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/csrf.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/ajax-form.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/default.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/prettify-min.js HTTP/1.1" 404 179

有大量静态文件通过API提供。如果我使用Postman或类似工具进行相同的操作,日志如下:

my_table    | [08/Jan/2023 20:25:12] "GET /api/v1/category/ HTTP/1.1" 200 2

看起来它只发送了我想要的响应,以JSON或其他方式。我想知道是否有办法阻止Django提供静态文件,因为我只会使用Rest Framework,或者将来可能使用gRPC,但绝不会使用静态文件。我尝试从设置中删除静态文件常量,然后一切都不再工作,所以我想知道是否需要采取一些措施来实现这个目标。感谢您的帮助。

英文:

I am quite new to Django, and I hope my question is not too silly. I am currently running my project locally, and each time I do an API call with the browser, I see the server log something similar:

my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/css/default.css HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/bootstrap.min.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/jquery-3.5.1.min.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/csrf.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/ajax-form.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/default.js HTTP/1.1" 404 179
my_table    | [08/Jan/2023 20:20:42] "GET /static/rest_framework/js/prettify-min.js HTTP/1.1" 404 179

There are a tons of static files that are served via the API. If I do the same with Postman or similar, here is the log:

my_table    | [08/Jan/2023 20:25:12] "GET /api/v1/category/ HTTP/1.1" 200 2

It looks like it only sends the response I wanted, via JSON or whatever. I was wandering if there is a way to prevent Django from serving static files at all, since I will only use the Rest Framework, or maybe gRPC in the future, but never static files. I tried to delete the static file constant from settings, and then nothing worked anymore, so I was wandering if there is something I need to do in order to achieve this goal. Thank you for your help

答案1

得分: 1

很简单。您进入已安装的应用程序,会找到类似以下内容的东西:

BASE_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

删除其中包含 django.contrib.staticfiles 的部分,然后您也可以删除静态文件根变量。

英文:

It is pretty simple. You go to the installed APPS,and you will find something like:

BASE_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Delete the one with django.contrib.staticfiles, and then you can delete also the static files root variables

huangapple
  • 本文由 发表于 2023年1月9日 03:29:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定