使用tango_with_django_project.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:

时间:2018-06-18 20:06:32

标签: django

我已经通过Tango使用Django 1.7指南,我已经提出了以下问题,我遇到了麻烦:

Using the URLconf defined in tango_with_django_project.urls, Django 
tried these URL patterns, in this order:
1. ^$ [name='index']
2. ^about/$ [name='about']
3. ^category/(?P<category_name_slug>[\w\-]+)/$ [name='category']
4. ^admin/
5. ^rango/$
6. ^about/
7. ^category/
8. ^media/(?P<path>.*)
The current URL, rango/category/python, didn't match any of these.

这是我的rango / urls.py文件

from django.conf.urls import patterns, url
from rango import views

urlpatterns = patterns(' ',
                   url(r'^$', views.index, name='index'),
                   url(r'^about/$', views.about, name='about'),
                   url(r'^category/(?P<category_name_slug>[\w\-]+)/$', 
views.category, name='category')
                   )

在tango_with_django_project / urls.py我有

from django.conf.urls import patterns, include, url
from django.contrib import admin
from rango import views

from django.conf import settings

urlpatterns = patterns('',
                   url(r'^$', views.index, name='index'),
                   url(r'^about/$', views.about, name='about'),

url(r'^admin/', include(admin.site.urls)),
url(r'^rango/$', include('rango.urls')),
url(r'^about/', include('rango.urls')),
)
if settings.DEBUG:
urlpatterns+= patterns(
    'django.views.static',
    (r'^media/(?P<path>.*)',
    'serve',
    {'document_root': settings.MEDIA_ROOT}),
)

在rango / views.py中我有:

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from rango.models import Category
from rango.models import Page

def index(request):

category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories': category_list}

return render(request, 'rango/index.html', context_dict)


def about(request):
return HttpResponse("Rango says: Hello world! <br/> <a             
href='/rango/about'>About</a>")


def category(request, category_name_slug):

context_dict = {}

try:

    category = Category.objects.get(slug=category_name_slug)
    context_dict['category_name'] = category.name

    pages = Page.objects.filter(category=category)

    context_dict['pages'] = pages

    context_dict['category'] = category
except Category.DoesNotExist:

return render(request, 'rango/category.html', context_dict)

我一直在关注指南,所以我不太确定问题是什么;我之前在指南中遇到了类似的问题,当我得到相同的错误但是有/约(从来没有得到这个修复)。 知道可能导致这个问题的原因是什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

正如已经评论过的,这里的问题是URLconf的定义,更准确地说是定义以rango/...开头的每个url的inclusion of another URLconf。关键是这一行:

url(r'^rango/$', include('rango.urls')),

应改为

url(r'^rango/', include('rango.urls')),

regex '^rango/$'抓取:<start-of-string>rango/<end-of-string>

正则表达式'^rango/'捕获:<start-of-string>rango/<whatever follows>,以及您所包含的网址所在的位置。