0

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?

1
  • The code you've provided doesn't work at all and it definitely doesn't raise that error. When I make any necessary changes it works fine. Commented Aug 9, 2017 at 0:54

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Looks like I just need to use 'index' instead of 'hello' on this line: url(r'^hello-world$', hello),
No problem. Yea, I figured that was the desired name, but changed it to index instead since that was the first one I saw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.