我正在学习如何创建api端点,并且正在尝试编写一个测试,以查看post请求是否返回200状态代码。我计划编写更多的测试,看看端点是否也返回了所有预期的结果。我一直得到一个403状态码,我想这是因为我需要在post数据中包含一个csrf令牌。在django中测试POST端点的好方法是什么?
我的测试:
from django.test import TestCase
from app import settings
import requests
class ProjectEndpoint(TestCase):
def post_endpoint(self):
data =
我正在我的Django项目中尝试测试和AJAX视图。当提交来自JQuery的帖子时,数据可以在Django视图中正确访问,但是当我试图查询Django测试客户端时,它是emplty的。
下面是我使用的代码:
视图
def add_item(request):
if request.is_ajax() and request.method == 'POST':
post_data = request.POST
print post_data ## <----- THIS IS EMPTY
name = post
这是我的forms.py file.We可以在forms.py中验证来自clean方法的表单数据
from django import forms
from django.core.exceptions import ValidationError
class ContactForm(forms.Form):
# Everything as before.
...
def clean_recipients(self):
data = self.cleaned_data['recipients&
我正在用一个建立在Piston上的API构建一个Django应用程序。为了保持一切尽可能干燥和API完整,我希望我的内部应用程序调用API而不是模型(类似于的代理-视图-控制器,但目前都在一个django安装上)。因此,基本设置为:
Model -> API -> Application -> User Client
我可以重载一些核心Piston类来为应用程序创建内部客户端接口,但我想知道是否可以只使用Django Test client来完成相同的事情。因此,为了创建一篇文章,而不是调用我要运行的模型:
from django.test.client import Cli
我写了Django应用程序,现在我正试图用自动化测试来覆盖它。为了在我的ListView中测试get_queryset函数,我创建了一个测试用户和他的帖子,但我的测试失败了,"No User matches the given query。当我执行py manage.py runserver时,一切都很好,没有异常,页面显示正常。我对Django测试是新手,所以我完全不知道发生了什么。你能帮我吗?
这是我在view.py上的观点
class UserPostListView(ListView):
"""Displaying a page with a certa
def login_user(request):
if request.method == "POST":
username = request.POST['username'] #this is my first field to be tested
password = request.POST['password'] #this is my second field to be tested
user = authenticate(username=username, passwor
视图函数示例: def example_view(request):
if request.method == "POST":
print(request.session['Key'])
return HttpResponse("Success") 测试用例到测试视图: from django.contrib.sessions.backends.db import SessionStore
from django.test import RequestFactory, TestCase
from we
我正在编写一个Django应用程序,它使用我创建的REST api。其目的是使用web应用程序来证明api用例。因此,在我看来,我使用python-request库调用api,如下所示:
def my_view_method(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
data = form.cleaned_data
data_to_post = {
'field
每当我在测试期间使用requestFactory时,如下所示:
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.client import Client
import nose.tools as nt
class TestSomeTestCaseWithUser(TestCase):
def setUp(self):
# Every te
我目前正在写一些基本的测试,以确保一个中等大小的Django应用程序中的页面是正确的GETting和POSTing。然而,使用django.test.client.Client并不会在它应该失败的时候可靠地失败。即使我的代码中有明显的错误,它也会返回302响应。
在我的app/urls.py中:
url(r'^mymodel/create/$',
views.MyModelView.as_view(),
name = 'my_model_create'),
然后,为了故意创建一个500响应,我执行了以下操作:
class MyModelCreateView(M
我想知道是否可以在Django视图方法中处理不同的POST请求。例如,使用以下代码:
def my_view(request):
if request.method == "POST":
if request.POST['value_one']:
# Do stuff here
elif request.POST['value_two']:
# Do stuff here
elif request.POST['value_three
因此,我正在使用Django进行TDD,我被困在了下面的问题上。
TestCase类的测试方法
def test_home_page_can_save_POST_request(self):
request = HttpRequest
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertIn('A new list item
我是新来姜戈的。我正在尝试运行一个应用程序,我需要添加一个新的用户到管理。服务器正在运行。当我输入新用户的信息并点击“保存”时,我得到了下面的错误。我正在使用django-trunk。
MessageFailure at /admin/auth/user/add/
You cannot add messages without installing
django.contrib.messages.middleware.MessageMiddleware
Request Method: POST
Request URL: http://localhost:8000/admin/auth/
最近我遇到了一个问题,我的django应用程序中的django图像字段在按"Post“之后没有更新图像。在管理面板中,一切都很好。也许我错过了什么..。让我知道。
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_lengt