django的静态文件配置和路由控制

时间:2023-03-09 15:31:44
django的静态文件配置和路由控制

上一篇写到刚建完django项目,此时我登录页面中调用了js文件,执行后发现报错了找不到js这个文件

目录结构如图所示:

django的静态文件配置和路由控制

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/statics/js.js"> </script>
</head>
<body> <h4>
登录页面
</h4>> <form action="" method="post">
用户名<input type="text" name="user">
密码<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>

显示找不到这个js文件

django的静态文件配置和路由控制

那么问题来了,我们应该怎么获取这个静态js文件呢?别急和我来一步一步配置

在django的公共项目目录中有一个settings.py的文件

在这个文件中配置一个STATICFILES_DIRS变量,配置好这个STATICFILES_DIRS后,django中就会把statics目录下的文件路径改成static

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/ STATIC_URL = '/static/' # django自带的 STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'statics'),
]

配置完参数后,html中引用的js路径改为/static/js/js

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/js.js"> </script>
</head>
<body> <h4>
登录页面
</h4>> <form action="" method="post">
用户名<input type="text" name="user">
密码<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>

再次执行接口,就找到这个js文件了

django的静态文件配置和路由控制

接下来讲一下路由配置,首先讲一下路由控制之简单配置

项目目录为:

django的静态文件配置和路由控制

在公共配置中的urls中添加路由配置

from django.contrib import admin
from django.urls import path, re_path
from website import views urlpatterns = [# 路由配置 路径--------》视图函数 re_path(r'^articles/2003/', views.special_case_2003),
re_path(r'^articles/([0-9]{4})/$', views.year_archive),
re_path(r'^articles/([0-9]{4})/([[0-9]{2})/$', views.month), ]

在单独项目对应的views(视图)文件中添加对应的视图函数

 from django.shortcuts import render, HttpResponse
import time # Create your views here. def special_case_2003(request): return HttpResponse('special_case') def year_archive(request, year): return HttpResponse(year) def month(request, year, months): return HttpResponse(year, months)

situation 1:

输入url为:http://127.0.0.1:8000/articles/2003/

输出结果为:

django的静态文件配置和路由控制

situation 2:

输入url为:http://127.0.0.1:8000/articles/2004/

输出结果为:

django的静态文件配置和路由控制

situation 3:

输入url为:http://127.0.0.1:8000/articles/2005/20/

输出结果为:

django的静态文件配置和路由控制

这里有个注意点需要强调一下!!!

当你的路由中有元组时,在对应的视图函数中就要加上一个对应的位置参数,这点很重要,否则就会报错。

rom django.contrib import admin
from django.urls import path, re_path
from website import views urlpatterns = [
path('admin/', admin.site.urls), # 路由配置 路径--------》视图函数 re_path(r'^articles/2003/', views.special_case_2003),
re_path(r'^articles/([0-9]{4})/$', views.year_archive), # 比如这个路径有一个元祖,在对应的year_archive中就需要对应的传一个位置参数
re_path(r'^articles/([0-9]{4})/([[0-9]{2})/$', views.month), # 这条也是 ] def year_archive(request, year): # 位置参数year return HttpResponse(year) def month(request, year, months): # 位置参数year和months return HttpResponse(months)

接下来讲下路由控制之有名分组

背景:当视图函数中的参数很多时,参数顺序很容易报错,这时就可以用到有名分组了。

路由控制:
re_path(r'^articles/(?P<y>[0-9]{4})/(?P<m>[[0-9]{2})/$', views.month) 对应的视图函数:
def month(request, m, y): # 这里必须要传y和m,否则会报错 return HttpResponse(y+'+'+m)

此时不管交换m和y的位置,对应的值都不会改变。

路由控制之分发

背景:之前写的路由都是写在全局中的,如果我有十几个应用我该怎么办?难道都写在全局中吗?显然不太现实,此时就要用到分发,把路由结偶。

1.先在自己的应用中创建一个urls文件,把对应的路由都写好。

django的静态文件配置和路由控制

from django.urls import path, re_path
from website import views urlpatterns = [ # 路由配置 路径--------》视图函数 re_path(r'^articles/2003/', views.special_case_2003),
re_path(r'^articles/([0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<y>[0-9]{4})/(?P<m>[[0-9]{2})/$', views.month), ]

2.在全局的urls文件中导入include

from django.contrib import admin
from django.urls import path, re_path, include
from website import views urlpatterns = [
path('admin/', admin.site.urls), # 分发
re_path(r'website/', include('website.urls')) # 第一个参数为应用根目录,第二个参数为应用的urls文件

3.此时输入http://127.0.0.1:8000/website/articles/2005/20/

输出结果为:

django的静态文件配置和路由控制

请尊重作者劳动成果,有需要请转载,标明出处!!!