路径转换器 <int:year>
默认情况下,以下路径转换器可用:
str
-匹配任何非空字符串,但路径分隔符除外'/'
。如果表达式中不包含转换器,则为默认设置。int
-匹配零或任何正整数。返回一个int。slug
-匹配由ASCII字母或数字以及连字符和下划线字符组成的任何条形字符串。例如,building-your-1st-django-site
。uuid
-匹配格式化的UUID。为防止多个URL映射到同一页面,必须包括破折号并且字母必须小写。例如,075194d3-6885-417e-a8a8-6c931e272f00
。返回一个UUID
实例。path
-匹配任何非空字符串,包括路径分隔符'/'
。这样一来,您就可以匹配完整的URL路径,而不仅仅是URL路径的一部分str.
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
在url匹配规则中使用正则表达式
需要导入 django.urls.re_path url规则: (?P<name>pattern) 其中name时组名(字段参数名),pattern是要匹配的模式
from django.urls import path, re_path
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-]+)/$', views.article_detail),
]
包含其它的URLconfs
方式一:在项目的根urls.py内可以配置多个app内自己的urls.py配置
# 项目目录下 urls.py 配置包含app自己的urls.py配置
from django.urls import include, path
urlpatterns = [
# ... snip ...
path('community/', include('aggregator.urls')), # app:aggregator
path('contact/', include('contact.urls')), # app: contact
# ... snip ...
]
方式二:通过使用 path()
实例的列表来包含其他 URL 模式
from django.urls import include, path
from apps.main import views as main_views
from credit import views as credit_views
# 自定义path列表
extra_patterns = [
path('reports/', credit_views.report),
path('reports/<int:id>/', credit_views.report),
path('charge/', credit_views.charge),
]
urlpatterns = [
path('', main_views.homepage),
path('help/', include('apps.help.urls')),
path('credit/', include(extra_patterns)), # 添加自定义列表
]
方式三:将url中前缀一直的规则写入到一个path配置中
from django.urls import include, path
from . import views
urlpatterns = [
path('<page_slug>-<page_id>/', include([
path('history/', views.history),
path('edit/', views.edit),
path('discuss/', views.discuss),
path('permissions/', views.permissions),
])),
]
静态文件配置
1:settings.py文件内 配置静态文件根目录地址;
2:项目名文件夹下的urls.py 内配置静态文件地址;
3:收集静态文件到配置的静态文件地址;
完成以上三步,这样正式环境启动服务后,才能加载出样式和图片等静态内容。
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
# uvicorn 启动报错问题:django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
# "D://my_git/demand_management/", # 指定静态文件绝对路径
)
# 项目名目录下的urls.py
from django.contrib import admin
from django.urls import path, include
from django.views import static ##新增
from django.conf import settings ##新增
from django.conf.urls import url ##新增
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'), # 新增
]
# 启动前静态文件有改动时执行以下命令,将静态文件生成到settings.py内的STATIC_ROOT位置
python manage.py collectstatic