Python 在Django中加载具有不同数据的相同模板时,如何防止url更改
我有一个简单的def,有这样的东西Python 在Django中加载具有不同数据的相同模板时,如何防止url更改,python,django,Python,Django,我有一个简单的def,有这样的东西 def policies_index(request): """Resource listing""" policies = Policy.objects.all() context = { 'policies' : policies } return render(request, "policies/index.html", context) def policies_date_filter(req
def policies_index(request):
"""Resource listing"""
policies = Policy.objects.all()
context = {
'policies' : policies
}
return render(request, "policies/index.html", context)
def policies_date_filter(request):
"""List the resources based on a month year filter"""
today = datetime.date.today()
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
status = settings.POLICY_STATUS_SELECT
for policy in policies:
for status_name in status:
if status_name['value'] == policy.status:
policy.status_name = status_name['name']
request.policies = policies
return policies_index(request)
def policies_index(request):
"""Resource listing"""
#If method == POST it means is the filter function so I change the policies queryset
if request.method == "POST":
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
else:
policies = Policy.objects.all()
context = {
'policies' : policies,
}
return render(request, "policies/index.html", context)
现在我想添加一种方法,使用月份和年份过滤这些信息,所以我制作了一个类似这样的函数
def policies_index(request):
"""Resource listing"""
policies = Policy.objects.all()
context = {
'policies' : policies
}
return render(request, "policies/index.html", context)
def policies_date_filter(request):
"""List the resources based on a month year filter"""
today = datetime.date.today()
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
status = settings.POLICY_STATUS_SELECT
for policy in policies:
for status_name in status:
if status_name['value'] == policy.status:
policy.status_name = status_name['name']
request.policies = policies
return policies_index(request)
def policies_index(request):
"""Resource listing"""
#If method == POST it means is the filter function so I change the policies queryset
if request.method == "POST":
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
else:
policies = Policy.objects.all()
context = {
'policies' : policies,
}
return render(request, "policies/index.html", context)
这样我就可以重用函数索引,避免编写再次打印视图的代码,而且效果很好,但是在url中,而不是像“/policies”这样的内容,我得到了像“/policies/datefilter”这样的内容
这是有意义的,因为我调用了另一个函数,有没有办法防止url更改?哈哈,写这个问题我刚想出来我可以在视图中调用相同的函数,但是使用POST方法而不是GET,这样他们就可以保持相同的url,所以我最终得到了这样的结果
def policies_index(request):
"""Resource listing"""
policies = Policy.objects.all()
context = {
'policies' : policies
}
return render(request, "policies/index.html", context)
def policies_date_filter(request):
"""List the resources based on a month year filter"""
today = datetime.date.today()
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
status = settings.POLICY_STATUS_SELECT
for policy in policies:
for status_name in status:
if status_name['value'] == policy.status:
policy.status_name = status_name['name']
request.policies = policies
return policies_index(request)
def policies_index(request):
"""Resource listing"""
#If method == POST it means is the filter function so I change the policies queryset
if request.method == "POST":
year = request.POST['year']
month = request.POST['month']
policies = Policy.objects.filter(end_date__year=year).filter(end_date__month=month)
else:
policies = Policy.objects.all()
context = {
'policies' : policies,
}
return render(request, "policies/index.html", context)
现在就像一个符咒,你们觉得呢?有更好的方法吗?我对Django有点陌生