在我的博客上发布的新闻的页面,是提供给任何用户。我使用pytest来检查页面是否可以被匿名用户访问。Url是通过使用新闻的id形成的,我将其传入地址参数(作为元组)。
结果我得到了这个类型错误。test_pages_availability[news:detail-news] - TypeError:需要字符串或类似字节的对象
我曾尝试将args格式化为str,在tuple格式的args后放置一个逗号,但没有帮助
试验规程
@pytest.mark.django_db
@pytest.mark.parametrize(
'name, args',
(
('news:detail', pytest.lazy_fixture('news')),
('news:home', None),
('users:login', None),
('users:logout', None),
('users:signup', None),
)
)
def test_pages_availability(client, name, args):
if args is not None:
url = reverse(name, args=(news.id,))
else:
url = reverse(name)
response = client.get(url)
assert response.status_code == HTTPStatus.OK
固定装置
@pytest.fixture
def news():
news = News.objects.create(
title='Новость',
text='Невероятное событие',
date=datetime.today,
)
return news
class NewsDetail(generic.DetailView):
model = News
template_name = 'news/detail.html'
def get_object(self, queryset=None):
obj = get_object_or_404(
self.model.objects.prefetch_related('comment_set__author'),
pk=self.kwargs['pk']
)
return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.user.is_authenticated:
context['form'] = CommentForm()
return context
追溯:
request = <FixtureRequest for <Function test_pages_availability[news:detail-news]>>
def fill(request):
item = request._pyfuncitem
fixturenames = getattr(item, "fixturenames", None)
if fixturenames is None:
fixturenames = request.fixturenames
if hasattr(item, 'callspec'):
for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
if val is not None and is_lazy_fixture(val):
item.callspec.params[param] = request.getfixturevalue(val.name)
值=<built-in method today of type object at 0x00007FFE9CA83650>
def parse_date(value):
"""Parse a string and return a datetime.date.
Raise ValueError if the input is well formatted but not a valid date.
Return None if the input isn't well formatted.
"""
match = date_re.match(value)
E类型错误:需要字符串或类似字节的对象
..\venv\lib\site-packages\django\utils\dataparse.py:75:类型错误
1条答案
按热度按时间thigvfpy1#
你的
data=
参数不是日期,它是一个函数将其转换为date
。你需要 * 调用 * 方法,所以:对于一个**
default=…
**[Django-doc],你确实可以传递一个 callable,Django随后会调用它,但不是作为一个值,这个值是函数调用的 result,而不是函数本身。