我正在开发一个webapp及其工作方式,当用户从复选框中选择值时,将显示其描述。我当前正在调用一个函数,而该函数又将调用另一个函数,我这样做是因为我想使用GET而不是POST,因为由于没有服务器端更改,因此GET比POST更好。如何使用使用GET和查询参数的单个函数来实现此目的?下面是我的代码。
这是我的 urls.py :
url(r'^view_task_description/$', views.view_task_description, name='view_task_description'),
url(r'^view_task_description/(?P<pk>[0-9]+)/$', views.get_task_description, name="get_task_description"),
这是我的 views.py :
@login_required
def view_task_description(request):
if request.method == 'POST':
task_description = GetTaskDescription(data=request.POST, user=request.user)
if task_description.is_valid():
obj = GetTaskDescription.get_task_description(task_description)
return redirect('get_task_description', pk=obj[0].pk)
return render(request, 'todoapp/select_task_description.html', context={'view_tasks': GetTaskDescription(user=request.user)})
@login_required
def get_task_description(request, pk):
obj = get_object_or_404(Task, pk=pk)
return render(request, 'todoapp/task_desc.html', context={'description': obj.description})
这是我的 forms.py :
class GetTaskDescription(forms.Form):
get_tasks = forms.ModelMultipleChoiceField(
queryset=Task.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=True
)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(GetTaskDescription, self).__init__(*args, **kwargs)
self.fields['get_tasks'].queryset = self.user.task_set.all()
def get_task_description(self):
tasks = self.cleaned_data['get_tasks']
return tasks
答案 0 :(得分:0)
在视图中更改表单数据的来源:
task_description = GetTaskDescription(data=request.GET, user=request.user)