Python 如何在使用django.TestCase时从视图中获取对象

Python 如何在使用django.TestCase时从视图中获取对象,python,django,unit-testing,templates,views,Python,Django,Unit Testing,Templates,Views,我正在尝试访问将发送到模板的对象“wines”。基本上,在这个例子中,我有一些葡萄酒是用瓶子、玻璃杯或两者都卖的。我参加的第一个测试应该能够检索发送到模板的名为“wines”的对象中的所有3种葡萄酒。(create_wine()是一个自定义wine.objects.create()方法) 如果您注意到了,我正在使用django.test导入TestCase,所以我有一个self.client对象要使用。另外,如果您注意到了,我将向您展示我试图在“(调试)”文本中取消ub的位置 我真正想要的是预渲

我正在尝试访问将发送到模板的对象“wines”。基本上,在这个例子中,我有一些葡萄酒是用瓶子、玻璃杯或两者都卖的。我参加的第一个测试应该能够检索发送到模板的名为“wines”的对象中的所有3种葡萄酒。(create_wine()是一个自定义wine.objects.create()方法)

如果您注意到了,我正在使用django.test导入TestCase,所以我有一个self.client对象要使用。另外,如果您注意到了,我将向您展示我试图在“(调试)”文本中取消ub的位置

我真正想要的是预渲染的json。在我看来,视图使用对象呈现html,以创建它需要的html并返回该html。那么如何访问这个预渲染对象呢

问题是我将使用相同的视图来渲染这些wine对象。如果可能的话,我希望使用相同的模板,这意味着将数据发送到视图并重写它,以便它在渲染之前获取正确的图像。我想这没关系。如果这打破了django的方法论,我洗耳恭听

还有别的办法吗?还是我离这里很近?

代码 意见 网址 美国犹他州 “结果”位于(调试) 简单模板wine_list.html
{%extends'wine/base.html%}
{%block content%}
{葡萄酒中的葡萄酒%}
{%endfor%}
{%endblock%}

您误解了测试客户端的工作方式。当您调用
self.client.get(“/wine/”)
时,它模拟对
/wine/
的请求,并调用您的
wine\u列表
视图。您不必手动调用
wine\u list

client.get()
调用返回一个响应。然后,您可以使用响应进行测试断言,并从中获取项目

def wine_list(request):
    wines = Wine.objects.filter(is_active=True)
    return render(request, 'wine/wine_list.html', {'wines': wines})
urlpatterns = [
    url(r'^$', 'wine.views.wine_list', name='wine_list'),
    url(r'^bottle/$', 'wine.views.wine_list', name='wine_list_bottle'),
    url(r'^glass/$', 'wine.views.wine_list', name='wine_list_glass'),
    url(r'^([0-9]+)/$', 'wine.views.wine_details', name='wine_detail'),
]
    from django.test import TestCase

    def test_both_wine_glass_and_bottle_pull_different_objects(self):

        # Todo: how to pull object info from view
        self.create_wine(container="bottle")
        self.create_wine(container="glass")
        self.create_wine(container="both")

        request = self.client.get("/wine/")
        from wine.views import wine_list
        result = wine_list(request)
(debug) result

        # assert wine/ wine is both glass and bottle
        # assert wine/glass/ wine is only both or glass wines
        # assert wine/bottle/ wine is only both or bottle wines
        self.fail("finish the test")
result = {HttpResponse} <HttpResponse status_code=200, "text/html; charset=utf-8">
 _charset = {NoneType} None
 _closable_objects = {list} <class 'list'>: []
 _container = {list} <class 'list'>: [b'<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>Wine List</title>\n    \n    <link rel="stylesheet" href="/static/wine/reset.css">\n    <link rel="stylesheet" href="/static/wine/menu.css">\n</head>\n<bod
 _handler_class = {NoneType} None
 _headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
 _reason_phrase = {NoneType} None
 charset = {str} 'utf-8'
 closed = {bool} False
 content = {bytes} b'<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>Wine List</title>\n    \n    <link rel="stylesheet" href="/static/wine/reset.css">\n    <link rel="stylesheet" href="/static/wine/menu.css">\n</head>\n<body>\n    <header c
 cookies = {SimpleCookie} 
 reason_phrase = {str} 'OK'
 status_code = {int} 200
 streaming = {bool} False
    self.create_wine(container="both")

    bottle = Wine.bottle.all()
    glass = Wine.glass.all()
    both = Wine.objects.all()

    self.assertEqual(2, len(bottle))
    self.assertEqual(2, len(glass))
    self.assertEqual(3, len(both))

def test_both_wine_glass_and_bottle_pull_the_same_template(self):
    bottle_list = self.client.get('/wine/bottle/')
    glass_list = self.client.get('/wine/glass/')
    both_list = self.client.get('/wine/')

    self.assertTemplateUsed(both_list, 'wine/wine_list.html')
    self.assertTemplateUsed(bottle_list, 'wine/wine_list.html')
    self.assertTemplateUsed(glass_list, 'wine/wine_list.html')
{% extends 'wine/base.html' %}
{% block content %}
    <section id="wine_content">
        <div class="cards">

            {% for wine in wines %}
            <div class="card">
                <a href="/wine/{{ wine.id }}">
                    <h4>{{ wine.name }}</h4>
                    <p>{{ wine.vintage }}</p>
                    <p>{{ wine.description}}</p>
                </a>
            </div>
            {% endfor %}

        </div>
    </section>

{% endblock %}
    response = self.client.get("/wine/")
    self.assertEqual(response.status_code, 200)  # check 200 OK response
    wines = response.context['wines']  # this is the list of wines you included in the context
    # check that wines is as you expected.
    for wine in wines:
        # All wines should be active
        self.assertTrue(wine.is_active)
    ...