I am working on a django project and when I try to run this function it returns that there can only be one argument given, but there is only one argument given.
from django.http import HttpResponse, Http404
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
def main_page(request):
template = get_template('main_page.html')
variables = Context({
'head_title':u'Bookmarks!',
'page_title':u'Welcome to bookmarks!',
'page_body': u'store and share the bookmarks',
})
output = template.render(variables)
return HttpResponse(output)
Url pattern
from django.conf.urls.defaults import patterns, include, url
from socialnetwork.bookmarks.views import *
urlpatterns = patterns('',
(r'^user/(\w+)/$', main_page),
)
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/user/Colin/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'socialnetwork.bookmarks']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /user/Colin/
Exception Value: main_page() takes exactly 1 argument (2 given)
I suspect that your URL pattern captures some other variable. If you attach a URL pattern to a view and that URL pattern captures any values, you need to have that view set up to take an argument for each captured value on top of the request argument, which is always first. So, let’s say you’ve got a pattern
myview()needs to be defined like so: