Rozumiem, że istnieje kilka pytań takich jak ten przepełnienie stosu, ale żaden z nich nie odpowiada na mój problem. Próbuję uruchomić test jednostkowy na moich stronach w projekcie, ale stawiam ten problem: "Odwróć do" Page1 "nie znaleziono." Page1 "nie jest poprawnym funkcją widoku lub nazwy wzoru."

"Page1" działa dobrze, gdy uruchomię go za pomocą python manage.py runserver, ale kiedy uruchomię python manage.py test nie powiedzie się.

Urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.displaylol),
    path('page1', views.page1),
    path('empty', views.empty),
]

Views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Tag

# Create your views here.

def displaylol(request):
    return HttpResponse("Home page")

def page1(request):
    tags = Tag.objects.all()
    return render(request, 'ecommerce/page1.html', {'tags': tags})

def empty(request):
    return HttpResponse("empty")

ECommerce / page1.html.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Product 1</title>
    </head>
    <body>
        <h1>Tags</h1>

        <table border="1">
            <thead>
                <tr>
                    <th>Tag</th>
                    <th>Connections</th>
                    <th>No. of products attached to</th>
                    <th>Idk</th>
                </tr>
            </thead>
            <tbody>
                {% for tag in tags %}
                <tr>
                    <td>
                        {{ tag.name }}<br>
                        <small style="color: #888">ID: {{ tag.ID }}</small>
                    </td>
                    <td>0</td>
                    <td>0</td>
                    <td></td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </body>
</html>

Testys.py.

from django.urls import reverse
from django.test import TestCase

# Create your tests here.

class page1Tests(TestCase):
    def test_home_view_status_code(self):
        url = reverse('page1') # It fails the same way when I use reverse('empty') as well
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)

Błąd:

(venv) xxx@xxx-MacBook 03_high_level % python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_home_view_status_code (ecommerce.tests.page1Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/03_high_level/ecommerce/tests.py", line 9, in test_home_view_status_code
    url = reverse('page1')
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/venv/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'page1' not found. 'page1' is not a valid view function or pattern name.

----------------------------------------------------------------------
Ran 1 test in 0.008s

FAILED (errors=1)
Destroying test database for alias 'default'...
1
icantcode 14 kwiecień 2021, 00:05

1 odpowiedź

Najlepsza odpowiedź

Musisz określić nazwę widoku, więc:

urlpatterns = [
    path('', views.displaylol),
    path('page1/', views.page1, name='page1'),
    path('empty/', views.empty),
]

W przeciwnym razie praca reverse('page1') nie .

0
Willem Van Onsem 14 kwiecień 2021, 06:39