Answer the question
In order to leave comments, you need to log in
How to return json using HttpResponce?
There is a view:
class CategoriesListView(ListView):
model = Category
queryset = Category.objects.all()
context_object_name = 'categories'
template_name = 'bikes_site/categories_list.html'
<ul>
{% for category in categories %}
<li> <a href="{{ category.get_absolute_url }}" class="btn btn-light mb-2" >{{ category.name|title }}</a></li>
{% endfor %}
</ul>
def test_categories(setup):
client = Client()
response = client.get('/categories/')
assert response.status_code == 200
assert json.loads(response.content.decode('utf-8'))[2]['name'] == 'Квадроциклы'
Answer the question
In order to leave comments, you need to log in
As I understand it, you are rendering html, and not giving pure json. You can get the rendered context with Context
response = client.get('/categories/')
response.context['name']
response = client.get('/categories/')
response.json()['name']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question