本文介绍了Django是否以某种方式缓存网址正则表达式模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是一个需要帮助的Django新手:即使我在我的urls.py中更改了一些URL,我仍然从Django获取相同的错误消息。以下是我的settings.py的相关行: ROOT_URLCONF ='mydjango.urls' 这是我的urls.py: from django.conf.urls.defaults import * #取消注释下面两行以启用admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('',#示例:#(r'^ mydjango /',include('mydjango.foo.urls')), #取消注释下面的admin / doc行,并将'django.contrib.admindocs'#添加到INSTALLED_APPS以启用管理员文档:#(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 (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)),) 所以我希望每当我访问 http://mydjango.yafz.org/polls/randomTest1/ mydjango.polls.views.randomTest1函数应该运行,因为在我的polls / views.py我有相关的功能: def randomTest1(request):#mainText = request.POST ['mainText'] return HttpResponse(Default random test) 但是,我继续收到以下错误消息: 没有找到(404)请求方法:GET 请求URL:http://mydjango.yafz。 org / polls / randomTest1 使用mydjango.urls中定义的URLconf,Django按照以下顺序尝试了这些URL模式: 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 / $ 当前的网址poll / randomTest1与以上任何一个都不匹配。 我很惊讶,因为一次又一次检查urls.py,没有 ^ polls / randomTest / $ ,但有 ^ polls / randomTest1 /' 似乎Django以某种方式存储urls.py的以前内容,我只是不知道如何使我的最新更改生效。 / p> 任何想法?即使我更改了我的urls.py,为什么我尝试加载该页面时仍然会看到一些旧版本的正则表达式?解决方案 p> Django在出现性能原因启动时会编译URL正则表达式 - 重新启动服务器,您应该会看到新的URL正常工作。 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 adminadmin.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: GETRequest URL: http://mydjango.yafz.org/polls/randomTest1Using 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? 解决方案 Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly. 这篇关于Django是否以某种方式缓存网址正则表达式模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 19:01