From 5dd8976f278c3672512fee6108655bdadfb2165f Mon Sep 17 00:00:00 2001 From: mxjdi Date: Fri, 4 Apr 2025 23:21:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AA=92=E4=BD=93=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E5=92=8C=E5=AA=92=E4=BD=93=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E6=AD=A3=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Django5/settings.py | 5 +++++ Django5/urls.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Django5/settings.py b/Django5/settings.py index df25714..b5bb881 100644 --- a/Django5/settings.py +++ b/Django5/settings.py @@ -115,6 +115,11 @@ STATIC_URL = 'static/' # 设置静态资源文件集合 # STATICFILES_DIRS = [BASE_DIR / 'static', BASE_DIR / 'helloworld/images'] +# 设置媒体路由 +MEDIA_URL = '/media/' +# 设置media目录的完整路径 +MEDIA_ROOT = BASE_DIR / 'media' + # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field diff --git a/Django5/urls.py b/Django5/urls.py index bd36cd4..8610e77 100644 --- a/Django5/urls.py +++ b/Django5/urls.py @@ -14,12 +14,16 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings 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 urlpatterns = [ path('admin/', admin.site.urls), path('index/', helloWorld.views.index), + # 媒体路由 + re_path('media/(?.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), ]