You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中如何实现单模型适配多视图,基于城市Slug共享数据?

解决方案

你的核心需求是让所有包含城市slug的页面都能共享对应City模型的数据,无需每个视图都重复绑定模型逻辑。可以通过统一的基础视图来实现,具体步骤如下:

1. 调整URL路由结构

把城市slug作为公共前缀,将所有相关页面的URL嵌套在这个前缀下,确保所有页面都能捕获到city_slug参数:

# appname/urls.py
from django.urls import path, include
from .views import CityBaseView, ServiceDetailView

urlpatterns = [
    path('<slug:city_slug>/', include([
        # 首页 domain.com/city/
        path('', CityBaseView.as_view(template_name='index.html'), name='city_home'),
        # 服务列表页 domain.com/city/services/
        path('services/', CityBaseView.as_view(template_name='services.html'), name='city_services'),
        # 服务详情页1 domain.com/city/services/service-detail/
        path('services/service-detail/', ServiceDetailView.as_view(), name='service_detail'),
        # 服务详情页2 domain.com/city/services/service-detail-2/
        path('services/service-detail-2/', CityBaseView.as_view(template_name='service_detail_2.html'), name='service_detail_2'),
    ])),
]

2. 编写基础视图类

创建一个继承自TemplateView的基础视图,负责从URL参数中获取对应的City实例,并将其注入到模板上下文中。所有需要城市数据的页面都可以复用这个视图:

# appname/views.py
from django.views.generic import TemplateView
from django.shortcuts import get_object_or_404
from .models import City

class CityBaseView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 通过URL中的city_slug获取对应的城市实例
        city = get_object_or_404(City, slug=kwargs['city_slug'])
        context['city'] = city
        return context

3. 扩展视图(可选)

如果某些页面需要额外的数据(比如服务详情页需要服务相关信息),可以继承CityBaseView并扩展上下文逻辑:

# appname/views.py
class ServiceDetailView(CityBaseView):
    template_name = 'service_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 这里可以添加服务相关的额外数据,比如从其他模型查询
        # context['current_service'] = Service.objects.get(city=context['city'], slug='service-1')
        return context

4. 在模板中使用城市数据

现在所有对应页面的模板里,都可以直接通过{{ city }}访问模型字段:

<!-- index.html 示例 -->
<h1>Добро пожаловать в {{ city.gorod }}</h1>
<p>Регион: {{ city.region }}</p>
<p>Телефон: {{ city.phone1 }}</p>

关于Laravel本地化方案的替代

Django中没有完全和Laravel本地化模块一致的工具,但如果你不想用数据库存储城市配置,可以考虑:

  • 使用Django的上下文处理器,从配置文件(比如settings.py中的字典)读取城市数据,通过URL参数匹配后注入模板
  • 自定义中间件,解析URL中的城市slug,将对应配置存入请求对象,供视图和模板调用

但你选择的模型方案更适合需要动态管理城市数据的场景,比如后续要添加/修改城市信息时,直接在后台操作即可,无需修改代码。

内容的提问来源于stack exchange,提问作者Игорь lockservice-pro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:45:05