Django Аргумент NoReverseMatch - PullRequest
0 голосов
/ 30 марта 2020

Не находит подходящий URL из списка (я полагаю, что для кортежа требуется строка)

NoReverseMatch at /decision/livingrooms/kitchen/
    Reverse for 'style' with arguments '('provans',)' not found. 1 pattern(s) tried:

['решение / жилые комнаты / (? P [- a-zA-Z0-9 _] +) / (? P [-a-zA-Z0-9 _] +) / $ ']

Шаблон

<ul class="menu menu_interiors">
            {% for room in rooms %}
                <li class="menu__item menu__item_interiors"><a href="{% url 'decision:room' room.slug %}">{{ room.name }}</a></li>
            {% endfor %}


        </ul>
<ul class="menu menu_styles">
            {% for style in styles %}
                <li class="menu__item menu__item_interiors"><a href="{% url 'decision:style' style.slug %}">{{ style.name }}</a></li>
            {% endfor %}
        </ul>

urls .py

from django.urls import path
from .views import ContactView
from . import views

app_name = 'decision'

urlpatterns = [
    path('livingrooms/', ContactView.as_view(), name='livingrooms'),
    path('livingrooms/<slug:rmslg>/', views.room, name='room'),
    path('livingrooms/<slug:rmslg>/<slug:stslg>/', views.style, name='style'),
]

views.py

def room(request, rmslg):
    styles = Room.objects.get(slug=rmslg).styles.all()
    print(len(styles))
    return render(request, 'decision/residentialInteriors.html', {"styles": styles})


def style(request, stslg):
    style = Style.objects.get(slug=stslg)
    return render(request, 'decision/residentialInteriors.html', {"style": style})

1 Ответ

0 голосов
/ 30 марта 2020

Изменить последний путь для принятия только одного параметра.

С:

path('livingrooms/<slug:rmslg>/<slug:stslg>/', views.style, name='style'),

Кому:

path('styles/<slug:stslg>/', views.style, name='style'), # Since your view only accepts one parameter, the URL should capture only one as well.

Окончательный результат:

urlpatterns = [
    path('livingrooms/', ContactView.as_view(), name='livingrooms'),
    path('livingrooms/<slug:rmslg>/', views.room, name='room'),
    path('styles/<slug:stslg>/', views.style, name='style'), # Since your view only accepts one parameter, the URL should capture only one as well.
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...