配置媒体文件夹和媒体路由,用正则

This commit is contained in:
mxjdi 2025-04-04 23:21:59 +08:00
parent fa36da700a
commit 5dd8976f27
2 changed files with 10 additions and 1 deletions

View File

@ -115,6 +115,11 @@ STATIC_URL = 'static/'
# 设置静态资源文件集合 # 设置静态资源文件集合
# STATICFILES_DIRS = [BASE_DIR / 'static', BASE_DIR / 'helloworld/images'] # STATICFILES_DIRS = [BASE_DIR / 'static', BASE_DIR / 'helloworld/images']
# 设置媒体路由
MEDIA_URL = '/media/'
# 设置media目录的完整路径
MEDIA_ROOT = BASE_DIR / 'media'
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field

View File

@ -14,12 +14,16 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, re_path
from django.views.static import serve
import helloWorld.views import helloWorld.views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('index/', helloWorld.views.index), path('index/', helloWorld.views.index),
# 媒体路由
re_path('media/(?<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
] ]