Django how to change static dir

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

Django how to change static dir

问题

Oh, I see you have a question about your project's static folder! 🌟 Well, it's like a little adventure in the land of code. Imagine your project as a magical forest, and the static folder is like a treasure chest of goodies for your apps!

So, you're wondering if you can move the static folder outside of the app folder, right? 🤔 Hmm, it's kind of like wanting to move a tree to a different part of the forest! But in the world of code, it's definitely possible. You can move the static folder wherever you like, as long as you update the paths in your code to point to the new location. It's like creating a secret path through the enchanted woods! 🌳✨

Just be sure to update the references to the static files in your JavaScript and CSS to match the new location, and your project should continue to sparkle with its magical size! 🌟💫

英文:

Default dir for static folder is project/app/static but since I'm using some js and css libs and I have multiple apps I don't want my project gain some extra size. I wonder if I could move the static folder outside of the app folder. Like:

| project
| | app
| | static

Is this possible?

答案1

得分: 1

如果您想将static根目录设置为BASE根目录,那么可以使用以下配置

settings.py

STATIC_URL = 'static/'
STATICFILES_DIRS = [BASE_DIR / 'static'] 

项目目录

Django how to change static dir

注意 - 使用**STATICFILES_DIRS = [BASE_DIR / 'static'],您可以设置自定义的静态根目录**,

只需将静态文件夹的相对路径设置在**STATICFILES_DIRS = [BASE_DIR / 'static文件夹的相对路径']**中。

英文:

If you want to set static root at BASE root then you can go with this config

settings.py

STATIC_URL = 'static/'
STATICFILES_DIRS = [BASE_DIR / 'static'] 

Project directory

Django how to change static dir

NOTE - using STATICFILES_DIRS = [BASE_DIR / 'static'] you can set custom static root,

only you need to set static folder relative path in STATICFILES_DIRS = [BASE_DIR / 'relative path of sttic folder']

huangapple
  • 本文由 发表于 2023年7月17日 16:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702571.html
匿名

发表评论

匿名网友

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

确定