我需要将id从一个模板传递到另一个模板。在模板中,我正在迭代一个模型
{% for project in all_projects %}
<h3>{{ project.name }}</h3>
<a href="{% url 'docs:platforms' %}?project={{ project.id }}"></a>
{% endfor %}
这将转到我的网址看起来像
的模板 url(r'^$', views.ProjectsListView.as_view(), name='index'),
url(r'^platforms/$', views.PlatformsIndexView.as_view(), name='platforms'),
url(r'^platforms/nodes/$', views.PlatformsNodesListView.as_view(), name='platforms_list'),
我拥有的浏览器网址是 http://127.0.0.1:8000/platforms/?project=1 这很好。但是从第二个模板我需要发送第三个模板另一个参数和过滤器。那我怎么能得到项目的ID?
我现在无法将项目ID发送到第三个模板,因为我没有迭代它。如何记住项目的ID?
views.py
class ProjectsListView(ListView):
template_name = 'project/projects.html'
model = Project
context_object_name = 'all_projects'
class PlatformsIndexView(TemplateView):
template_name = 'project/platforms.html'
class PlatformsNodesListView(ListView):
template_name = 'project/general.html'
model = Platform
context_object_name = 'all_platforms'
def get_queryset(self):
queryset = super().get_queryset()
type_filter = self.request.GET.get('type')
project_filter = self.request.GET.get('project')
if type_filter in [Platform.BACKEND, Platform.ANDROID, Platform.IOS, Platform.FRONTEND]:
queryset = queryset.filter(type=type_filter)
if project_filter:
queryset = queryset.filter(project__id__exact=project_filter)
else:
raise Http404
return queryset
请解释一下。 提前谢谢