我正在开发一个Web应用程序,但似乎无法执行在重定向方法中使用过的视图。有人可以帮我吗?
这是我的 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/task_desc.html', context={'description': obj[0].description})
return render(request, 'todoapp/select_task_description.html', context={'view_tasks': GetTaskDescription(user=request.user)})
@login_required
def get_task_description(request, pk):
# print "Reached get task descrition method"
obj = get_object_or_404(Task, pk=pk)
return render(request, 'todoapp/task_desc.html', context={'description': obj.description})
这是我的网址:
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"),
这是我的 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 :(得分:1)
您需要在第一个正则表达式中添加一个$
,否则它将与/view_task_description/5/
和/view_task_description
都匹配。
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"),
我还添加了一个斜杠,以匹配通常的Django样式和您的其他URL模式。