【问题标题】:Reverse for 'restaurants_list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []未找到带有参数“()”和关键字参数“{}”的“restaurants_list”的反向操作。尝试了 0 种模式:[]
【发布时间】:2018-07-12 06:57:20
【问题描述】:

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

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)

经过这么多努力还是找不到这个错误的正确解决方法。非常需要帮助。在此先感谢

【问题讨论】:

  • 在主页上的RedirectView 中使用permanent=True 之前请仔细考虑。如果它是永久性的,那么如果您将来决定更改它,它可能会导致问题。

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


【解决方案1】:

您的问题是您的网址是restaurant_list 而不是restaurants_list

在您的模板中使用myrestaurants:restaurant_list 而不是restaurants_list,一切都应该没问题

&lt;form method="GET" class="form-inline" action="{% url 'myrestaurants:restaurant_list' %}"&gt;

【讨论】:

  • 好地方,但我认为您还需要添加包含命名空间,例如'myrestaurants:restaurant_list'.
  • 感谢 Angel F 指出我的愚蠢错误,@Alasdair 你告诉我的问题是正确的解决方案。stackoverflow 的新手不知道如何为评论投票,但对我来说 +100。
  • @Vaibhav Goyal 我已经更新了很多答案,我没有看到他们的命名空间,但你是正确的,如果设置了命名空间,当然,最好的方法是使用命名空间 + urlname跨度>
  • @Angel F 在 url 之前没有命名空间对我不起作用我不知道为什么但仍然。顺便感谢更新答案
猜你喜欢
  • 2016-11-21
  • 2014-11-23
  • 2017-01-06
  • 2016-05-08
  • 2016-08-27
  • 2017-05-04
  • 1970-01-01
  • 2016-03-21
  • 2014-01-26
相关资源
最近更新 更多