未定义/ global name'refray_to_reponse'处的NameError

时间:2011-11-11 21:53:16

标签: django

user@ubuntu:~/Documents/Django/django_bookmarks$ tree
.
├── bookmarks
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
├── bookmarksdb
├── __init__.py
├── manage.py
├── settings.py
├── templates
│   ├── main_page.html
│   ├── registration
│   │   └── login.html
│   └── user_page.html
├── urls.py

# content of view.py
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext


def user_page(request, username):
    try:
        user = User.objects.get(username = username)
    except User.DoesNotExist:
        raise Http404(u'Requested user not found.')

    bookmarks = user.bookmark_set.all()

    template = get_template('user_page.html')
    variables = Context({
       'username' : username,
       'bookmarks' : bookmarks
    })

    output = template.render(variables)
    return HttpResponse(output)

def main_page(request):
    #template = get_template('main_page.html')
    initialData = {'user', request.user}
    csrfContext = RequestContext(request, initialData)
    #variables = Context({'user' : request.user})
    #output = template.render(variables)

    #return HttpResponse(output)
    return render_to_reponse('main_page.html', csrfContext)


user@ubuntu:~/Documents/Django/django_bookmarks$ cat urls.py
from django.conf.urls.defaults import patterns, include, url
from bookmarks.views import *

urlpatterns = patterns('',
    (r'^$', main_page),
    (r'^user/(\w+)/$', user_page),
    (r'^login/$', 'django.contrib.auth.views.login'),
)


user@ubuntu:~/Documents/Django/django_bookmarks$ cat templates/registration/login.html 
<html>
  <head>
    <title>Django Bookmarks - User Login</title>
  </head>

  <body>
    <h1>User Login</h1>
    {% if form.has_errors %}
    <p>Your username and password didn't match.
      Please try again.</p>
    {% endif %}
    <form method="post" action=".">
      {% csrf_token %}
      <p><label for="id_username">Username:</label>
    {{ form.username }}</p>
      <p><label for="id_password">Password:</label>
    {{ form.password }}</p>
      <input type="hidden" name="next" value="/" />
      <input type="submit" value="login" />
    </form>
  </body>
</html>

问题&GT;我遇到了一个错误,我无法弄清楚原因。

NameError at /

global name 'render_to_reponse' is not defined

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.3.1
Exception Type:     NameError
Exception Value:    

global name 'render_to_reponse' is not defined

Exception Location:     /home/user/Documents/Django/django_bookmarks/../django_bookmarks/bookmarks/views.py in main_page, line 62
Python Executable:  /usr/bin/python
Python Version:     2.7.1
Python Path:    

['/home/user/Documents/Django/django_bookmarks',
 '/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1-py2.7-linux-i686.egg',
 '/usr/local/lib/python2.7/dist-packages/mongoengine-0.5.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/elementtree-1.2.7_20070827_preview-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Markdown-2.0.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/django_mumblr-0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pip-1.0.2-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/pymodules/python2.7/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/pymodules/python2.7/ubuntuone-storage-protocol',
 '/usr/lib/pymodules/python2.7/ubuntuone-control-panel',
 '/usr/lib/pymodules/python2.7/ubuntuone-client']

Server time:    Fri, 11 Nov 2011 15:46:29 -0600

1 个答案:

答案 0 :(得分:6)

render_to_reponse未定义。它应该是render_to_response

请注意响应中缺少的“s”