使用参数'()'和关键字参数'{}'找不到'restaurants_list'的反转。尝试过0种模式:[]

时间:2018-02-01 20:43:35

标签: python django django-models django-views django-urls

需要帮助无法纠正此错误:

NoReverseMatch at /myrestaurants/
Reverse for 'restaurants_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/myrestaurants/
Django Version: 1.8.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'restaurants_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: /usr/lib/python2.7/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 495
Python Executable:  /usr/bin/python
Python Version: 2.7.12
Python Path:    
['/home/vaibhav/Desktop/projects/myrecommendations',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/vaibhav/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/gtk-2.0']
Server time:    Fri, 2 Feb 2018 02:02:23 +0000

我的index.html如下所示:

{% block content %}
        <nav class="nav nav-bar">
            <div class="pull-right" style="padding: 20px;">
                {% if user.is_anonymous %}
                    <a href="{% url 'account_login' %}">Sign In</a>
                    <a href="{% url 'account_signup' %}">Sign Up</a>
                {% else %}
                    <a href="{% url 'account_logout' %}">Sign Out</a>
                {% endif %}
            </div>
        </nav>
        <div class="container">
            <h1 class="app-font">Welcome to Food-orders</h1>
            <form method="GET" class="form-inline" action="{% url 'restaurants_list' %}">
                <input type="text" id="locator" class="form-control">
                {% buttons %}
                <button type="submit" class="btn btn-success"/>
                    {% bootstrap_icon "glyphicon glyphicon-search" %} SEARCH FOR RESTAURANTS
                </button>
                {% endbuttons %}
            </form>
        </div>
{% endblock %}

我的myrestaurants / urls.py看起来像这样:

from django.utils import timezone

from django.conf.urls import url
from django.views.generic import ListView, DetailView, UpdateView

import myrestaurants
from myrestaurants import views
from myrestaurants.forms import RestaurantForm, DishForm
from myrestaurants.models import Restaurant, Dish
from myrestaurants.views import RestaurantDetail, RestaurantCreate, DishCreate, RestaurantUpdate, DishUpdate

urlpatterns = [
    url(r'^$', views.HomePageView.as_view(), name='home-page'),
    #url(r'^list/$', views.RestaurantListView.as_view(), name='restaurant-list'),
    # List latest 5 restaurants: /myrestaurants/
    url(r'^myrestaurants/$',
        ListView.as_view(
            queryset=Restaurant.objects.filter(date__lte=timezone.now()).order_by('date')[:8],
            context_object_name='latest_restaurant_list',
            template_name='myrestaurants/restaurant_list.html'),
        name='restaurant_list'),

    # Restaurant details, ex.: /myrestaurants/restaurants/1/
    url(r'^restaurants/(?P<pk>\d+)/$',
        RestaurantDetail.as_view(),
        name='restaurant_detail'),

    # Restaurant dish details, ex: /myrestaurants/restaurants/1/dishes/1/
    url(r'^restaurants/(?P<pkr>\d+)/dishes/(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Dish,
            template_name='myrestaurants/dish_detail.html'),
        name='dish_detail'),

    # Create a restaurant, /myrestaurants/restaurants/create/
    url(r'^restaurants/create/$',
        RestaurantCreate.as_view(),
        name='restaurant_create'),

    # Edit restaurant details, ex.: /myrestaurants/restaurants/1/edit/
    url(r'^restaurants/(?P<pk>\d+)/edit/$',
        RestaurantUpdate.as_view(),
        name='restaurant_edit'),

    # Create a restaurant dish, ex.: /myrestaurants/restaurants/1/dishes/create/
    url(r'^restaurants/(?P<pk>\d+)/dishes/create/$',
        DishCreate.as_view(),
        name='dish_create'),

    # Edit restaurant dish details, ex.: /myrestaurants/restaurants/1/dishes/1/edit/
    url(r'^restaurants/(?P<pkr>\d+)/dishes/(?P<pk>\d+)/edit/$',
        DishUpdate.as_view(),
        name='dish_edit'),

    # Create a restaurant review, ex.: /myrestaurants/restaurants/1/reviews/create/
    # Unlike the previous patterns, this one is implemented using a method view instead of a class view
    url(r'^restaurants/(?P<pk>\d+)/reviews/create/$',
        myrestaurants.views.review,
        name='review_create'),
]

我的myrecommendations / urls.py看起来像这样:

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth.views import login, logout
from django.views.static import serve
from django.conf import settings
from django.views.generic import RedirectView
from myrestaurants import urls as restaurants_urls


urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', RedirectView.as_view(url='myrestaurants', permanent=True)),
    url(r'^$', RedirectView.as_view(url='restaurant_list', permanent=True)),
    url(r'^myrestaurants/', include('myrestaurants.urls', namespace="myrestaurants")),
    url(r'^accounts/', include('allauth.urls')),
]
#if settings.DEBUG:
urlpatterns += [
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, })
]

我的views.py看起来像这样:

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse, reverse_lazy
from django.views.generic import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.base import TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from myrestaurants.forms import DishForm, RestaurantForm
from myrestaurants.models import Restaurant, RestaurantReview, Dish

class HomePageView(TemplateView):
    template_name = "myrestaurants/index.html"

class RestaurantDetail(DetailView):
    model = Restaurant
    template_name = 'myrestaurants/restaurant_detail.html'

    def get_context_data(self, **kwargs):
        context = super(RestaurantDetail, self).get_context_data(**kwargs)
        context['RATING_CHOICES'] = RestaurantReview.RATING_CHOICES
        return context


class RestaurantCreate(CreateView):
    model = Restaurant
    template_name = 'myrestaurants/form.html'
    form_class = RestaurantForm
    success_url = reverse_lazy("myrestaurants:restaurant_list")

    def form_valid(self, form):
        form.instance.user = self.request.user
        name = form.cleaned_data['name']
        messages.success(self.request, '"%s"vaibhav' % name)
        return super(RestaurantCreate, self).form_valid(form)

在努力尝试之后无法找出错误并正确解决此错误。需要帮助。谢谢提前

1 个答案:

答案 0 :(得分:3)

您的问题是,您调用的是您的网址restaurant_list而不是restaurants_list

在模板中使用myrestaurants:restaurant_list代替restaurants_list,一切正常

<form method="GET" class="form-inline" action="{% url 'myrestaurants:restaurant_list' %}">