Captain Kirk
Captain Kirk

Reputation: 1196

Python URL Not Found

I am using this Python tutorial but looks like the author hasn't updated it because the code is using deprecated Python code, specifically:

from django.conf.urls import patterns

    urlpatterns = patterns('',
        (r'^hello-world/$', index),
    )

I looked up Django documentation and other StackOverFlow questions but I am unable to resolve how I can use the current method. This is what I have added to replace the above:

urlpatterns = [
    url(r'^hello-world$', hello),
]

And here is the full hello.py file:

import sys

from django.conf import settings
from django.conf.urls import url
from django.http import HttpResponse
from django.core.management import execute_from_command_line

settings.configure(
    DEBUG=True,
    SECRET_KEY='thisisabadkeybutitwilldo',
    ROOT_URLCONF=sys.modules[__name__],
)

def index(request):
    return HttpResponse('Hello, World')

import views

urlpatterns = [
    url(r'^hello-world$', hello),
]

if __name__ == "__main__":
    execute_from_command_line(sys.argv)

And this is the error message I get from Python:

Using the URLconf defined in main, Django tried these URL patterns, in this order: ^$ [name='hello'] The current URL, hello, didn't match any of these.

What am I doing wrong?

Upvotes: 0

Views: 314

Answers (1)

Cory Madden
Cory Madden

Reputation: 5193

Your code, as provided, does not work. I tested the code from the linked tutorial, and it works fine in 1.8. Your code has a random import(import views) and an undefined variable(hello). So I made the logical edits to your code and tested it and it's fine in 1.11:

import sys

from django.conf import settings
from django.conf.urls import url
from django.http import HttpResponse
from django.core.management import execute_from_command_line

settings.configure(
    DEBUG=True,
    SECRET_KEY='thisisabadkeybutitwilldo',
    ROOT_URLCONF=sys.modules[__name__],
)

def index(request):
    return HttpResponse('Hello, World')

urlpatterns = [
    url(r'^hello-world$', index),
]

if __name__ == "__main__":
    execute_from_command_line(sys.argv)

Upvotes: 1

Related Questions