为什么Django上的反向操作无法解析此url?
我有一个更新条目的方法:为什么Django上的反向操作无法解析此url?,django,django-urls,Django,Django Urls,我有一个更新条目的方法: @require_POST def update(request, uuid): posti = get_object_or_404(Post, uuid=uuid) f = PostForm(request.POST) if f.is_valid(): posti.title = request.POST['title'] posti.text = request.POST['text'] pos
@require_POST
def update(request, uuid):
posti = get_object_or_404(Post, uuid=uuid)
f = PostForm(request.POST)
if f.is_valid():
posti.title = request.POST['title']
posti.text = request.POST['text']
posti.save()
return HttpResponseRedirect(django.urls.reverse('posti:detail', args=(posti.uuid,)))
else:
messages.error(request, "No title in form")
return render(request, 'posti/detail.html', {'form': f})
我试图用一个案例来测试这个方法:
def create_post():
"""
:rtype : Post
"""
data = {'title': 'Esto es un titulo', 'text': 'Esto es un texto de prueba'}
post = Post.create_post(data['title'], data['text'])
post.save()
print('The UUID is: ', post.uuid)
return post
def test_updating_post_with_no_title(self):
"""
A post with no title should not be saved
:return:
"""
p = create_post()
print('(test) The UUID is: ', p.uuid)
data = {'title': '', 'text': 'Esto es un texto de prueba'}
url = reverse('posti:update', args=(p.uuid,))
response = self.client.post(url, data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No title in form")
无论我做什么,我总是会遇到这样的错误:
django.url.exceptions.NoReverseMatch:与“更新”相反
未找到参数“(“”,)”。尝试了1个模式:
['posti/update/(?P[0-9a-zA-Z-\]+)/$']
在两种方法中使用print(p.UUID)
时,UUID确实会出现在测试中,但它没有从下到上到达第四行。如果它说“with arguments”(“”),“not found”,则应该显示UUID。为什么它不显示它
编辑:UUID是一个小型UUID。我不知道这是否有问题 因为“+”匹配一个或多个。要解释一下吗?我不明白。我在另一个页面上使用了相同的正则表达式,但它确实在读取错误时起作用?问题是,在运行更新时,参数没有显示出来。所以它就像是在计算一个空值。我需要弄清楚的是为什么这个值没有显示出来。