Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Django cache url regex patterns somehow?

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?

like image 393
Emre Sevinç Avatar asked Apr 29 '10 12:04

Emre Sevinç


People also ask

How Django URLs work with regular expressions?

We use regular expressions to match these url paths to their corresponding request handler (aka View). If a path, either dynamic or static, is matched the View will then handle the request and return a response to the User. If a path is not matched, Django will automatically send a 404 Page Not Found response.

How does Django cache work?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.

What is URL patterns in Django?

In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject.

Does Django have caching?

For convenience, Django offers different levels of cache granularity: You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site. Django also works well with “downstream” caches, such as Squid and browser-based caches.


1 Answers

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

like image 97
Frozenskys Avatar answered Sep 26 '22 17:09

Frozenskys