Y
Y
YuriyCherniy2020-02-28 23:00:47
Django
YuriyCherniy, 2020-02-28 23:00:47

Why does reverse behave strangely when testing a View with a POST request?

Here is the tested view:

class MainPageEditorCreate(LoginRequiredMixin, View):
    raise_exception = True

    def get(self, request):
        if ItemOnMainPage.objects.count() == 9:
            messages.warning(
                request, 'Нельзя добавить на главную больше 9 товаров'
            )
            return redirect('main_page_editor_url')

        form = ItemOnMainPageCreateForm()
        return render(
            request, 'mainpage/item_on_main_page_form.html', {'form': form}
        )

    def post(self, request):
        form = ItemOnMainPageCreateForm(request.POST)
        if form.is_valid():
            try:
                ItemOnMainPage.objects.create(
                    item_on_main_page=form.cleaned_data['item_on_main_page'],
                    position=ItemOnMainPage.objects.count() + 1
                )
                messages.success(request, 'Товар успешно добавлен на главную')
                return redirect('main_page_editor_url')
            except IntegrityError:
                messages.warning(request, 'Этот товар уже есть на главной')
                return redirect('main_page_editor_url')
        else:
            messages.warning(request, 'Что-то пошло не так!')
            return redirect(request, '/')

Here is my non-working test:
def test_main_page_editor_create_view_status_code_200_POST(self):
        self.c.post(
            '/admin/login/', {'username': 'test', 'password': '0000'}
        )
        response = self.c.post(
            reverse('add_item_to_main_page_url'),
            {'item_on_main_page_id': 1}
        )
        self.assertEquals(response.status_code, 200)

This is how Django complains:
'<WSGIRequest: POST '/main-page-editor/add-item/'>' not found. '<WSGIRequest: POST '/main-page-editor/add-item/'>' is not a valid view function or pattern name.

Why does it reversereturn such a strange looking url: '/main-page-editor/add-item/'>'What is the greater than sign at the end that is causing the test to fail? When testing a GET request, this does not happen, the url is returned without extra characters.
Just in case, here is my urlpatterns:
urlpatterns = [
    path('', MainPageEditorList.as_view(), name='main_page_editor_url'),
    path('<int:pk>/delete/', MainPageEditorDelete.as_view(), name='delete_item_from_main_page_url'),
    path('<int:pk>/update/', MainPageEditorUpdate.as_view(), name='update_item_on_main_page_url'),
    path('add-item/', MainPageEditorCreate.as_view(), name='add_item_to_main_page_url')
]

Tell me where to dig? I can't figure out what I'm stuck on.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noremorse_ru, 2020-03-13
@noremorse_ru

The invalid is triggered and request is returned return redirect(request, '/')
here superfluous
In general, of course, it would be easier to take the ready-made FormView class, and messages to transmit errors from the forms, the code will be cleaner and clearer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question