django - 为什么这个 url 在 Django 上没有被反向解析?
问题描述
我有一个更新条目的方法:
@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.urls.exceptions.NoReverseMatch:没有找到带有参数“(”,)“的“更新”的反向。尝试了 1 种模式:['posti/update/(?P[0-9a-zA-Z-_]+)/$']
UUID 确实出现在使用print(p.uuid)
这两种方法的测试中,但它没有到达从下到上的第四行。它说“未找到参数'('',)'”的地方应该出现一个UUID。为什么不显示它?
编辑:UUID 是 SmallUUID。我不知道这是否有问题。
解决方案
推荐阅读
- .htaccess - 可以在未找到的消息中注入文本
- ios - 获取接入点列表及其信号强度
- vba - 这个语法是什么意思?
- javascript - React(单元和集成):用于反应单元测试和集成测试的框架
- python - 如何使用 Python 将系统生成的日志转换为图像?
- c# - .Net 中的执行上下文和仅反射上下文是什么?
- python - 单元测试似乎工作......但某些超过 13 位的大整数输入失败
- android - 有人可以帮助我了解成功侦听器的firebase逻辑吗
- java - 如何从一个 for 循环中抛出线程?
- excel - Excel VBA:如何在application.undo之后在更改事件中记录用户按键?