Does Django cache url regex patterns somehow?

橙三吉。 提交于 2019-12-10 15:56:28

问题


I'm a Django newbie who needs help: Even though I change some urls in my urls.py I keep on getting the same error message from Django. Here is the relevant line from my settings.py:

ROOT_URLCONF = 'mydjango.urls'

Here is my urls.py:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^mydjango/', include('mydjango.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    #(r'^admin/doc/', include(django.contrib.admindocs.urls)),

    # (r'^polls/', include('mydjango.polls.urls')),
    (r'^$', 'mydjango.polls.views.homepage'),
    (r'^polls/$', 'mydjango.polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'mydjango.polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'mydjango.polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'mydjango.polls.views.vote'),
    (r'^polls/randomTest1/', 'mydjango.polls.views.randomTest1'),
    (r'^admin/', include(admin.site.urls)),
)

So I expect that whenever I visit http://mydjango.yafz.org/polls/randomTest1/ the mydjango.polls.views.randomTest1 function should run because in my polls/views.py I have the relevant function:

def randomTest1(request):
    # mainText = request.POST['mainText']
    return HttpResponse("Default random test")

However I keep on getting the following error message:

Page not found (404)
Request Method:     GET
Request URL:    http://mydjango.yafz.org/polls/randomTest1

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

   1. ^$
   2. ^polls/$
   3. ^polls/(?P<poll_id>\d+)/$
   4. ^polls/(?P<poll_id>\d+)/results/$
   5. ^polls/(?P<poll_id>\d+)/vote/$
   6. ^admin/
   7. ^polls/randomTest/$

The current URL, polls/randomTest1, didn't match any of these.

I'm surprised because again and again I check urls.py and there is no

 ^polls/randomTest/$

in it, but there is

 ^polls/randomTest1/'

It seems like Django is somehow storing the previous contents of urls.py and I just don't know how to make my latest changes effective.

Any ideas? Why do I keep on seeing some old version of regexes when I try to load that page even though I changed my urls.py?


回答1:


Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly.



来源:https://stackoverflow.com/questions/2737400/does-django-cache-url-regex-patterns-somehow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!