动态 Django url 重定向到错误的函数
Dynamic Django urls redirects to the wrong function
所以我想做的是创建 urls,这样用户就可以通过传递特定参数来查看投诉,或者如果没有传递参数则默认按赞成票排序。
urls.py
path('', views.exploreComplaints, name='explore-complaints'),
path('?sort_by=<str:sorting_parameter>/', views.exploreComplaints, name='explore-complaints-by-parameter'),
views.py
def exploreComplaints(request, sorting_parameter="upvotes"):
complaints = Complaint.objects.all()
if(sorting_parameter=="name"):
complaints = sorted(complaints, key = lambda x : x.complaint_name)
else:
complaints = sorted(complaints, key = lambda x : x.complaint_upvotes, reverse = True)
context = {'complaints':complaints}
return render(request, 'complaints/complaints.html', context)
当我转到 URL 时,排序参数不起作用,sorting_parameter
的值始终是赞成票,即使当我转到 url 和 ?/sort_by=name
到底。我哪里错了?
编辑:
投诉型号:
class Complaint(models.Model):
complaint_title = models.CharField(max_length = 1000, null = True)
complaint_filer = models.ForeignKey(User, null = True, on_delete = models.CASCADE)
complaint_description = models.TextField(null = True)
complaint_status = models.CharField(max_length = 10, default = 'active')
complaint_request_image = models.ImageField(null = True, blank = True, upload_to = 'complaints', default = 'complaints/default-image.jpg')
complaint_place = models.CharField(max_length = 1000, null = True)
complaint_under_investigation_by = models.CharField(max_length = 1000, null = True, blank = True)
complaint_upvotes = models.IntegerField(default = 0)
complaint_upvotes_users = models.TextField(default=',', blank = True)
complaint_downvotes_users = models.TextField(default=',', blank = True)
tags = TaggableManager()
def __str__(self):
return self.complaint_title
Django URL 调度程序 doesn't look at GET or POST parameters 同时解析处理程序。
您需要在视图中访问 request.GET
映射:Django request get parameters
在您的情况下,视图定义如下所示:
urls.py
urlpatterns = [
path('', views.exploreComplaints, name='explore-complaints'),
]
views.py
def exploreComplaints(request):
complaints = Complaint.objects.all()
sorting_parameter = request.GET.get("sort_by")
if sorting_parameter == "name":
complaints = sorted(complaints, key=lambda x: x.complaint_name)
else:
complaints = sorted(complaints, key=lambda x: x.complaint_upvotes, reverse=True)
context = {'complaints':complaints}
return render(request, 'complaints/complaints.html', context)
# or, to offload sorting on the database:
def explore_complaints(request):
# distinguish the model field to sort by
sorting_parameter = request.GET.get("sort_by")
ordering_param = "-complaint_upvotes" # default ordering
if sorting_parameter == "name":
ordering_param = "complaint_name"
# add ORDER BY clause to the final query
complaints = Complaint.objects.order_by(ordering_param)
context = {"complaints": complaints}
return render(request, "complaints/complaints.html", context)
所以我想做的是创建 urls,这样用户就可以通过传递特定参数来查看投诉,或者如果没有传递参数则默认按赞成票排序。
urls.py
path('', views.exploreComplaints, name='explore-complaints'),
path('?sort_by=<str:sorting_parameter>/', views.exploreComplaints, name='explore-complaints-by-parameter'),
views.py
def exploreComplaints(request, sorting_parameter="upvotes"):
complaints = Complaint.objects.all()
if(sorting_parameter=="name"):
complaints = sorted(complaints, key = lambda x : x.complaint_name)
else:
complaints = sorted(complaints, key = lambda x : x.complaint_upvotes, reverse = True)
context = {'complaints':complaints}
return render(request, 'complaints/complaints.html', context)
当我转到 URL 时,排序参数不起作用,sorting_parameter
的值始终是赞成票,即使当我转到 url 和 ?/sort_by=name
到底。我哪里错了?
编辑: 投诉型号:
class Complaint(models.Model):
complaint_title = models.CharField(max_length = 1000, null = True)
complaint_filer = models.ForeignKey(User, null = True, on_delete = models.CASCADE)
complaint_description = models.TextField(null = True)
complaint_status = models.CharField(max_length = 10, default = 'active')
complaint_request_image = models.ImageField(null = True, blank = True, upload_to = 'complaints', default = 'complaints/default-image.jpg')
complaint_place = models.CharField(max_length = 1000, null = True)
complaint_under_investigation_by = models.CharField(max_length = 1000, null = True, blank = True)
complaint_upvotes = models.IntegerField(default = 0)
complaint_upvotes_users = models.TextField(default=',', blank = True)
complaint_downvotes_users = models.TextField(default=',', blank = True)
tags = TaggableManager()
def __str__(self):
return self.complaint_title
Django URL 调度程序 doesn't look at GET or POST parameters 同时解析处理程序。
您需要在视图中访问 request.GET
映射:Django request get parameters
在您的情况下,视图定义如下所示:
urls.py
urlpatterns = [
path('', views.exploreComplaints, name='explore-complaints'),
]
views.py
def exploreComplaints(request):
complaints = Complaint.objects.all()
sorting_parameter = request.GET.get("sort_by")
if sorting_parameter == "name":
complaints = sorted(complaints, key=lambda x: x.complaint_name)
else:
complaints = sorted(complaints, key=lambda x: x.complaint_upvotes, reverse=True)
context = {'complaints':complaints}
return render(request, 'complaints/complaints.html', context)
# or, to offload sorting on the database:
def explore_complaints(request):
# distinguish the model field to sort by
sorting_parameter = request.GET.get("sort_by")
ordering_param = "-complaint_upvotes" # default ordering
if sorting_parameter == "name":
ordering_param = "complaint_name"
# add ORDER BY clause to the final query
complaints = Complaint.objects.order_by(ordering_param)
context = {"complaints": complaints}
return render(request, "complaints/complaints.html", context)