英文:
While testing routes of my django prodject, i got Type error: expected sting or bites like object. How i can fix this error?
问题
我的博客上发布的新闻页面对任何用户都可用。我使用pytest检查页面是否对匿名用户可访问。
URL由新闻的ID组成,我将其传递给地址参数(作为元组)。
在结果中,我得到了这个类型错误。test_pages_availability[news:detail-news] - TypeError: 期望字符串或类似字节的对象
我尝试过将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
类NewsDetail(generic.DetailView)的代码
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)
>
value = <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 TypeError: expected string or bytes-like object
..\venv\lib\site-packages\django\utils\dateparse.py:75: TypeError
英文:
The page of the published news on my blog, is available to any user. I use pytest to check if the page is accessible to an anonymous user.
Url is formed by using the id of the news, which I pass in the address parameters (as a tuple).
In the result I got this Type error. test_pages_availability[news:detail-news] - TypeError: expected string or bytes-like object
I had tried to format args to str, puted a coma after arg for tuple format, it didn't helped
code of test
@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
`
fixture
@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
Traceback:
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)
>
value = <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 TypeError: expected string or bytes-like object
..\venv\lib\site-packages\django\utils\dateparse.py:75: TypeError
答案1
得分: 0
您的data=
参数不是一个日期,而是一个将其转换为date
的函数。您需要调用该方法,因此:
@pytest.fixture
def news():
return News.objects.create(
title='Твой заголовок',
text='Текст новости о чем-то интересном',
date=datetime<b>.today()</b>,
)
对于default=…
[Django-doc],您确实可以传递一个Django将调用的可调用对象,但不是作为值,而是该函数调用的结果,而不是函数本身。
英文:
Your data=
parameter is not a date, it is a function to convert it into a date
. You need to call the method, so:
<pre><code>@pytest.fixture
def news():
return News.objects.create(
title='Новость',
text='Невероятное событие',
date=datetime<b>.today()</b>,
)</code></pre>
For a <code>default=…</code> <sup>[Django-doc]</sup> you can indeed pass a callable that Django will then call, but not as a value, the value is then the result of the function call, not the function itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论