- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,在DeleteView中,GET请求返回一个确认页面,并且除了csrf_token
之外没有任何字段的简单POST请求实际上获取DeleteView来删除对象,用户将被重定向到 success_url
。
如何测试此功能?在我的 myclass_confirm_delete.html
文件中,我基本上有:
<form action="{% url 'remove_myclass' object.id %}" method="post">
{% csrf_token %}
<p>Are you sure you want to remove {{ object.name }}?</p>
<input type="submit" value="Yes" class="btn btn-primary" />
</form>
其中 {% url 'remove_myclass' object.id %}
是同一页面的 URL。它可以在我的浏览器中运行。当我单击"is"时,它会将我重定向到成功页面,并且 myclass
对象将从数据库中删除。
现在我正在尝试通过单元测试自动测试它。我基本上尝试过
response = self.client.get(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertContains(response, 'Are you sure you want to remove') # THIS PART WORKS
self.client.post(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertRedirects(response, reverse('myclass_removed'), status_code=302) # FAILS; status code is 200
如果我尝试打印响应
,我会得到与使用 GET 请求时完全相同的响应。
似乎在单元测试时,无论我尝试在 POST 请求中发送哪种数据,它仍然会被视为 GET 请求...
我的基于类的 View :
class MyclassDelete(DeleteView):
model = myclass
success_url = '/myclass-removed/'
有什么想法吗?
最佳答案
是的,这是因为您忘记将 post
请求分配给 response
,因此您检查了相同的响应两次。
response = self.client.get(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertContains(response, 'Are you sure you want to remove') # THIS PART WORKS
post_response = self.client.post(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertRedirects(post_response, reverse('myclass_removed'), status_code=302)
这应该可以解决问题。
另外,只是一个提示,在单元测试时尝试在每个单元测试中多次断言被认为是不好的做法。相反,尝试将其分解,以便一个测试测试 GET
,另一个测试测试 POST
。
from django.test import TestCase
class TestDifferentRequestMethods(TestCase):
def test_my_get_request(self):
response = self.client.get(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertContains(response, 'Are you sure you want to remove') # THIS PART WORKS
def test_my_post_request(self):
post_response = self.client.post(reverse('remove_myclass', args=(myobject.id,)), follow=True)
self.assertRedirects(post_response, reverse('myclass_removed'), status_code=302)
这使得调试更加容易,并且有时可以在遇到此类麻烦时节省时间!
更新意识到我没有完成一个很好的类(class)来完成测试。
关于Django 测试DeleteView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006401/
我正在使用与文档中解释的完全一样的 DeleteView https://docs.djangoproject.com/en/2.0/ref/class-based-views/generic-edi
是否有一些优雅的解决方案来使用 Django 的 DeleteView 而不是实际删除对象,将它们标记为非事件状态?我喜欢 DeleteView 的简单性,但希望将数据保留在后端,而不是将其删除。 最
我有一个删除 View : class LectureDelete(SuccessMessageMixin, DeleteView): model = Lecture success_message
我正在尝试编写一个 DeleteView 来删除帖子而不显示确认页面。 Del - delete button .如何立即删除对象? urls.py: urlpatterns = [ # ur
我刚刚学习了 CreateView、UpdateView 和 DeleteView 模型,然后我让它们全部工作起来。我可以更改我的对象,我可以删除它们,但是在删除一个对象后,我尝试创建另一个对象,并创
我正在使用 Django 中的 DeleteView 删除不同模型中的对象。 问题是我不希望对象被完全删除,而只是隐藏起来。首先,我认为保留我的 View 是有意义的,而不是重写每个模型中的 dele
我找到了这个 DeleteView。任何人都可以告诉我 return HttpResponseNotAllowed(['POST']) 是做什么用的?我是否也应该将它添加到我自己的 DeleteVie
我在模板中使用 Django DeleteView,并创建了一个 url 和 View 。是否可以跳过加载_confirm_delete模板的过程并立即发布删除。 最佳答案 DeleteView 响应
这个问题在这里已经有了答案: Example of Django Class-Based DeleteView (4 个答案) 关闭 6 年前。 我正在尝试从特定客户端删除报告,因此目前在我的 ur
我有一个 DeleteView。我想在其中访问相关的对象 ID。当用户发出 POST 请求时。 查看: class DeletePacketView(LoginRequiredMixin, Delet
我有一个包含 2 个条件“帖子”和“用户”的删除 View 。 用户需求由 self.object.user = self.request.user 满足,帖子需求由 slug = self.kwar
我有几个模型,Content 和 Hostname。 Hostname 有一个关联模型的 content_id: class Content(models.Model): name = mod
我有这些模型。每个回复可以没有、一个或多个帖子。帖子是特定于用户的。如何创建删除 View ,以便用户只能删除自己的帖子,而不能删除其他人回复的帖子。我尝试了很多次,但我的观点是删除其他用户的帖子。意
因此,我尝试为我的 Lecture 模型设置一个 DeleteView。我的 Course 模型中的每门类(class)都有自己的 slug 页面。该页面上会出现类(class)讲座。 问题是我尝试为
我需要用JS确认,而不是django的HTML确认。 Here我找到了问题的解决方案,但没有代码示例。请帮助并给我一些代码。这是我的观点: class EmployeeDelete(DeleteVie
有没有人知道或可以请任何人制作一个 Django 基于类的通用 DeleteView 的简单示例?我想继承 DeleteView 并确保当前登录的用户在删除之前拥有该对象的所有权。任何帮助将不胜感激。
我正在尝试实现 DeleteView 并防止在用户不是所有者的情况下删除模型。我敢肯定这是一项非常微不足道的任务,但我无法找到明确的答案。到目前为止,我可以看到我的 View ,但目前每个人都可以删除
在用户删除一个 PieceInstance 后,我想将他重定向到 PieceInstance 所属的 Piece。我使用默认的 DeleteView 并希望将 Piece id(通过 ForeignK
我希望在单击删除按钮时创建一个确认弹出窗口。它目前正在运行并立即删除。这是我的一些代码: View .py: class patientDelete(LoginRequiredMixin, Delet
我是 django 的新手,我遇到了 ajax 的问题。我有下面的代码。 我有一个 django delte View 。我想将它与 ajax 一起使用。现在 View 正在运行,但 CSRF tok
我是一名优秀的程序员,十分优秀!