解耦URLConfs(Python/Django):当前URL在错误之后不匹配任何URL模式

时间:2021-09-02 10:43:30

I have just started teaching myself python/django and have been following the django tutorials. I have got stuck at the decoupling of URLConfs. I have copied the code to respective files from the tutorial but I am getting this error:

我刚刚开始自学python/django,并且一直在学习django教程。我陷入了对URLConfs的解耦。我已经将代码从教程中复制到相应的文件中,但是我得到了这个错误:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

    ^polls/ ^/$
    ^polls/ ^/(?P<poll_id>\d+)/$
    ^polls/ ^/(?P<poll_id>\d+)/results/$
    ^polls/ ^/(?P<poll_id>\d+)/vote/$
    ^admin/
The current URL, polls/, didn't match any of these.

I have got urls.py in both mysite and polls folder as asked by the tutorial (https://docs.djangoproject.com/en/dev/intro/tutorial03/)

我有网址。根据教程的要求,在mysite和polling文件夹中的py (https://docs.djangoproject.com/en/dev/intro/tutorial03/)

1 个解决方案

#1


2  

Short answer: remove the first / in the URL configs on polls.urls.

简短的回答:删除poll .urls上的URL configs中的第一个/。

Long explanation: The problem here, as Julian mentioned in the comments is that you have an extra slash in the URL configs.

长解释:这里的问题是,正如朱利安在评论中提到的,URL configs中有一个额外的斜杠。

When you have the following in your root URL config:

当您在根URL配置中有以下内容时:

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

This essentially 'chops off' the polls/ part of the URL string and passes the rest on to polls.urls. So if you try and get the URL polls/13/results, the polls/ will be handled by my site.urls and the rest will be given to polls.urls.

这本质上是“砍掉”轮询/ URL字符串的一部分,并将其余部分传递给poll .urls。因此,如果您尝试获得URL投票/13/结果,投票/将由我的站点来处理。url和其余部分将被提供给poll .urls。

Your polls.urls file seems to be requiring a preceding / on the string however, and because this has been removed already it doesn't exist. The caret ^ character means the beginning of the string so all of your configs at the moment require that they start with a / at the point that polls.urls receives them, they shouldn't do this otherwise you are expecting the following:

你的投票。不过,url文件似乎需要字符串前面的/,因为已经删除了,所以它不存在。插入符号^字符表示字符串的开始所以你所有的配置目前要求他们首先/民意调查。url接收它们,它们不应该这样做,否则你会期望以下内容:

/polls//13/results

instead of:

而不是:

/polls/13/results

I hope this answers everything!

我希望这能解答一切!

#1


2  

Short answer: remove the first / in the URL configs on polls.urls.

简短的回答:删除poll .urls上的URL configs中的第一个/。

Long explanation: The problem here, as Julian mentioned in the comments is that you have an extra slash in the URL configs.

长解释:这里的问题是,正如朱利安在评论中提到的,URL configs中有一个额外的斜杠。

When you have the following in your root URL config:

当您在根URL配置中有以下内容时:

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

This essentially 'chops off' the polls/ part of the URL string and passes the rest on to polls.urls. So if you try and get the URL polls/13/results, the polls/ will be handled by my site.urls and the rest will be given to polls.urls.

这本质上是“砍掉”轮询/ URL字符串的一部分,并将其余部分传递给poll .urls。因此,如果您尝试获得URL投票/13/结果,投票/将由我的站点来处理。url和其余部分将被提供给poll .urls。

Your polls.urls file seems to be requiring a preceding / on the string however, and because this has been removed already it doesn't exist. The caret ^ character means the beginning of the string so all of your configs at the moment require that they start with a / at the point that polls.urls receives them, they shouldn't do this otherwise you are expecting the following:

你的投票。不过,url文件似乎需要字符串前面的/,因为已经删除了,所以它不存在。插入符号^字符表示字符串的开始所以你所有的配置目前要求他们首先/民意调查。url接收它们,它们不应该这样做,否则你会期望以下内容:

/polls//13/results

instead of:

而不是:

/polls/13/results

I hope this answers everything!

我希望这能解答一切!