python版本:3.7.9
mysqlclient版本:1.4.6
Django版本:3.2.13
问题说明:部署好Django后,映射到域名,访问静态资源(js、css等)提示404
解决方案:
settting.py代码
DEBUG = False ALLOWED_HOSTS = ['*'] STATIC_URL = "/static/" STATIC_ROOT = os.path.join(BASE_DIR, "static")
urls.py代码
from django.conf import settings from django.urls import path, re_path from django.views.static import serve from App01.views import index, admin, goods, user, other_views urlpatterns = [ # 此处不用添加static的路由 ]
映射到的网站的配置文件:
server { location ^~ /static { alias /www/wwwroot/django_1/static/; } }
之后重启Django,清除浏览器缓存即可