Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Django URL.py正则表达式没有像我预期的那样工作_Regex_Django_Django Views_Django Urls - Fatal编程技术网

Regex Django URL.py正则表达式没有像我预期的那样工作

Regex Django URL.py正则表达式没有像我预期的那样工作,regex,django,django-views,django-urls,Regex,Django,Django Views,Django Urls,我想有一个像这样的网址-- …每一年都是一年。所以我在我的应用程序的url.py中添加了这一行-- 这是我的观点-- 正如你所看到的,这只是目前的基本情况,只是一种测试,以确保事情在我进一步研究之前顺利进行。404页上写着-- Django使用msite.URL中定义的URLconf,按以下顺序尝试了这些URL模式: ^管理员/ ^帐目/ ^图表/^$[name='index'] ^chart/^chart/(?P\d+)$[name='chart'] ^健康/ 当前URL chart/2010

我想有一个像这样的网址--

…每一年都是一年。所以我在我的应用程序的url.py中添加了这一行--

这是我的观点--

正如你所看到的,这只是目前的基本情况,只是一种测试,以确保事情在我进一步研究之前顺利进行。404页上写着--

Django使用msite.URL中定义的URLconf,按以下顺序尝试了这些URL模式:
^管理员/
^帐目/
^图表/^$[name='index']
^chart/^chart/(?P\d+)$[name='chart']
^健康/
当前URL chart/2010与其中任何一个都不匹配。

我猜您已经在项目级(即根)urlconf中包含了“图表”URI组件,因此再次包含它会使应用程序的urlconf脱离解析程序。基本上尝试从图表url中删除“chart/”,如下所示:

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

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^(?P<year>\d+)$',views.chart,name="chart"),
)
来自django.conf.url导入模式,url
从音乐图表导入视图
urlpatterns=模式(“”,
url(r'^$',views.index,name=“index”),
url(r'^(?P\d+)$,views.chart,name=“chart”),
)

此外,当从根urlconf中包含应用程序级urlconf时,请仔细检查“chart”后面是否没有尾随空格。

chart函数是否使用
year
参数?即使没有,他也不会得到404错误,404表示URL不匹配。404给出了可以与实际URL匹配的URL列表。你能发布404页面报告的实际URL吗?你能显示模板中的用法吗?你能显示完整的URL定义吗?天哪,我真不敢相信我没听清楚。我一直在想这一定是一个regex的错误,因为我以前从来没有这样做过,所以我完全忽略了真正的问题。谢谢
url(r'^chart/(?P<year>\d+)$',views.chart,name="chart"),
from django.conf.urls import patterns,url
from musichart import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^chart/(?P<year>\d+)$',views.chart,name="chart"),
)
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader #Context
from musichart.models import Station,Song,Album,Related,Artist


def index(request):
    template = loader.get_template('chart/index.htm')
    context = RequestContext(request, {
        'title': "Here is the title",
        'testvar': "blah blah blah testing 1 2 3",
        'numero': 17,
    })
    return HttpResponse(template.render(context))


def chart(request, year):
    template = loader.get_template('chart/chart.htm')
    context = RequestContext(request, {
        'title': "Here is the title",
        'testvar': "blah blah blah testing 1 2 3",
        'numero': 17,
    })
    return HttpResponse(template.render(context))
Using the URLconf defined in msite.urls, Django tried these URL patterns, in this order:
^admin/
^accounts/
^chart/ ^$ [name='index']
^chart/ ^chart/(?P<year>\d+)$ [name='chart']
^health/
The current URL, chart/2010, didn't match any of these.
from django.conf.urls import patterns,url
from musichart import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^(?P<year>\d+)$',views.chart,name="chart"),
)