1

I'm just getting started with Django and am using djangobook.com. I tried the dynamic URL examples but it is giving me a TypeError. Can you see what's wrong ?

views.py

from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import datetime

def nameOffset(request, offset):
    print "in nameOffset"
    t = get_template('base.html')
    html = t.render(Context({'name':offset}))
    return HttpResponse(html)

urls.py

from django.conf.urls import patterns, include, url
from MemberInterface.views import getName, nameOffset

urlpatterns = patterns('', 
    (r'^name/$', getName ),
    (r'^name/plus/\d+/$', nameOffset ),
)

Everything is fine at /localhost/name/

But when I go to /localhost/name/plus/1/, I get

TypeError at /name/plus/1/

nameOffset() takes exactly 2 arguments (1 given)

Request Method:     GET Request URL:    /localhost/name/plus/1/ 
Django  Version:    1.5.1 Exception Type:   TypeError Exception Value:  

nameOffset() takes exactly 2 arguments (1 given)

What does it mean by "2 arguments, one given" .. the arugments are request and offset... and isn't request internally passed with the get ?

EDIT:

This is the base.html

<html>
<title> Test Project </title>
<body>
Hello {{ name }}
</body>
</html>
1
  • Links are written as localhost/name , etc 'cause SO doesn't let me type in 127.0, etc Commented Jun 26, 2013 at 8:27

2 Answers 2

2

Thank you all for the help. I figured it out. Posting it here in case anyone else has the same issue

The documentation at https://docs.djangoproject.com/en/dev/topics/http/urls/ mentions that anything that needs to be captured from the url needs to be in parenthesis. (I guess the pdf of the djangobook needs updating)

So, in urls.py, the line should be

(r'^name/plus/(\d+)/$', nameOffset ),

instead of

(r'^name/plus/\d+/$', nameOffset ),

And finally, it works !

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

Comments

1

You should use regex saving named groups in order to capture that \d+ part of the url into the offset variable:

(r'^name/plus/(?P<offset>\d+)/$', nameOffset)

Also see documentation.

6 Comments

That gets rid of the error PAGE but not the error. As in, the page should read "Hello 123" but it reads "Hello". I believe that's called a silent exception ?
Could you show your base.html? Plus, print the offset variable inside the view - do you see the value printed?
Added base.html. Also, offset isn't printed -_-
Hm, strange. What is printed instead?
Nothing... It should say "Hello 123" but the number is just ignored. All it says is "Hello"
|

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.