4

I'm trying to learn to use the django REST framework via the tutorial. I've gotten to the section "Testing our first attempt at a Web API".

When I start the server, I get:

System check identified no issues (0 silenced). June 15, 2015 - 00:34:49 Django version 1.8, using settings 'tutorial.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404

But when I do the next step: /Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/

I get an error which says, among other things:

You're seeing this error because you have <code>DEBUG = True</code> in your
  Django settings file. Change that to <code>False</code>, and Django will
  display a standard page generated by the handler for this status code.

So, when I change settings.py to say:

DEBUG = False
ALLOWED_HOSTS = ['*']

And then start the server, this happens: In the first terminal window:

^$ python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
June 15, 2015 - 00:52:29
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Then in the second terminal window:

$ /Library/Frameworks/Python.framework/Versions/3.3/bin/http http://127.0.0.1:8000/snippets/
HTTP/1.0 500 Internal Server Error
Content-Length: 59
Content-Type: text/plain
Date: Mon, 15 Jun 2015 00:52:42 GMT
Server: WSGIServer/0.2 CPython/3.3.5
A server error occurred.  Please contact the administrator.

And simultaneously, in the first terminal window, a bunch of stuff ending in:

File "/tutorial/tutorial/urls.py", line 9, in <module>
    url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined

I don't understand how to get this to work. I see that at least one person has asked a similar question about the "DEBUG=False" setting in the context of their own project, but frankly the answer is over my head. Can someone please explain this in the context of the tutorial?

Many thanks!

1 Answer 1

2

This error:

File "/tutorial/tutorial/urls.py", line 9, in <module>
    url(r'^admin/', include(snippets.urls)),
NameError: name 'snippets' is not defined

occurs because snippets.urls has to be quoted. See the example here:

urlpatterns = [
    url(r'^', include('snippets.urls')),
]

As shown here, you could write something like this:

from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

Note how the second include() does not contain a quoted argument. That's allowable because the import statement makes a variable called admin available and it has an attribute named site, which in turn has an attribute named urls; and whatever the value of admin.site.urls is, it's a valid argument for the include() function.

But if you write:

from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(snippets.urls))
]

Then python looks around for a variable named snippets, and because it can't find one, python gives you the error:

NameError: name 'snippets' is not defined

...

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard page generated by the handler for this status code.

In the phrase will display a standard page generated by the handler for this status code, the status code means error code, which means that you still will get the error, but you will not get any description of the error. When you are in production mode, i.e. not development mode, you don't want to display error messages to users who will have no idea what they mean, and in any case will have no means to correct the errors. So, in production mode you set DEBUG=False.

However, when you are developing you want verbose error messages to display when something goes wrong. Otherwise, you are going to see a webpage that says:

Sorry something went wrong: 500 error. Goodbye.

which is of absolutely no help to you in tracking down the error. Leave Debug=True.

When I start the server, I get:

System check identified no issues (0 silenced).
June 15, 2015 - 00:34:49
Django version 1.8, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[15/Jun/2015 00:35:17]"GET /snippets/ HTTP/1.1" 500 74404

You didn't have to proceed any further because the last line there indicates an error. The status code of the response to the GET request was 500. If you search google for http status codes, you will learn that a 500 status code means that something went wrong on the server side, and therefore the server was unable to respond normally to the request, i.e. your code has an error in it.

Sign up to request clarification or add additional context in comments.

Comments

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.