diff --git a/docs/img/StaticReadme/1.png b/docs/img/StaticReadme/1.png new file mode 100755 index 0000000000000000000000000000000000000000..4377fece3c006b12fb601faee1c23a6e7b982207 Binary files /dev/null and b/docs/img/StaticReadme/1.png differ diff --git a/docs/img/StaticReadme/2.png b/docs/img/StaticReadme/2.png new file mode 100755 index 0000000000000000000000000000000000000000..366d4f97b6c1a3f9ba2731e54f04a6bbfef9bdb2 Binary files /dev/null and b/docs/img/StaticReadme/2.png differ diff --git a/static_url_init/README.md b/static_url_init/README.md index 1da589c44f2aab53e1ffcc1997628d8270ab8e3d..c19f3326325943c91555c0b248207e4b1889a07c 100644 --- a/static_url_init/README.md +++ b/static_url_init/README.md @@ -27,4 +27,21 @@ python3 manage.py runserver - 设置ip为`127.0.0.1` - 设置port为8099 -### +## 三、静态文件配置 + +### 3.1 静态资源路径设置 + +在`config/settings.py`中通过设置STATIC_URL变量值使得后期可以通过`http://192.168.3.50:8099/static/`访问静态资源路径,不过设置的目录名必须是app下的,项目根目录下的没用,例如,我们可以设置`STATIC_URL = 'img/'`,则表示我们访问`http://192.168.3.50:8099/static/`该链接,对应的是`app/img`路径,不过我们通常保持默认的设置,即如下设置: + +```python +STATIC_URL = 'static/' +``` + +### 3.2 静态文件访问测试 + +在app目录下创建static目录,并将avatar.png图像放入该目录下,同时将cat.jpg图像放入static/img目录下,然后分别访问链接:http://192.168.3.50:8099/static/avatar.png 和 http://192.168.3.50:8099/static/img/cat.jpg + +![image-20250101000842701](../docs/img/StaticReadme/1.png) + +![image-20250101001027819](../docs/img/StaticReadme/2.png) + diff --git a/static_url_init/app/static/avatar.png b/static_url_init/app/static/avatar.png new file mode 100755 index 0000000000000000000000000000000000000000..c75a45e67805c45d63e485ed572710ad72f85d8e Binary files /dev/null and b/static_url_init/app/static/avatar.png differ diff --git a/static_url_init/app/static/img/cat.jpg b/static_url_init/app/static/img/cat.jpg new file mode 100755 index 0000000000000000000000000000000000000000..473e85b333aadd5bf5108d159966964845521d1e Binary files /dev/null and b/static_url_init/app/static/img/cat.jpg differ diff --git a/static_url_init/app/views.py b/static_url_init/app/views.py index 91ea44a218fbd2f408430959283f0419c921093e..42a3a2fb2d7e30e02ee2fd2d6374f488bf24efd1 100644 --- a/static_url_init/app/views.py +++ b/static_url_init/app/views.py @@ -1,3 +1,6 @@ -from django.shortcuts import render +from django.shortcuts import render, HttpResponse # Create your views here. + +def index(request, *args, **kwargs): + return HttpResponse("Hello, world!")