django登录异常值:'unicode'对象不可调用

时间:2015-07-23 01:38:12

标签: django twitter-bootstrap

我正在尝试使用django和bootstrap设置登录herehere

my views.py

def LoginRequest(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = authenticate(email=email, password=password)
            if user is not None:
                login(request, user)
                return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
            else:
                return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
        else:
            return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
    else:
        form = LoginForm()
        return render_to_response('/', {'form': form}, context_instance=RequestContext(request))

my urls.py

from core.views import LoginRequest, LogoutRequest, ContactRequest

urlpatterns = patterns('',
    url(r'^login/$', 'LoginRequest'),
    url(r'^logout/$', 'LogoutRequest'),

my forms.py

class LoginForm(forms.Form):
    email = forms.CharField(label=(u'Email'), max_length=30)
    password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False), max_length=30)

来自我的index.html的片段 -

      {% if user.is_authenticated %}
        <li><a href={% url 'dashboard' %}>Dashboard</a></li>
      {% else %}
        <li class="dropdown">
        <a class="dropdown-toggle" href="#" data-toggle="dropdown">Login<strong class="caret"></strong></a>
        <div class="dropdown-menu" style="padding: 15px; padding-bottom: 10px;">
          <form action='/login/' method="POST">
          {% csrf_token %}
            {% if form.errors %}
                {{ form.errors}}
            {% endif %}
            <label for="id_email">Login:</label>
            <input type="text" name="email" id="id_email">
            <br />
            <label for="id_password">Pass:</label>
            <input type="password" name="password" id="id_password">
            <br />
            <input class="btn btn-primary" type="submit" value="Login" />
            <input type="hidden" name="next" value="{{ '/dashboard' }}" />
        </form>
       </div>
       </li>
     {% endif %}

一旦我点击“登录”,我就会得到以下追溯 -

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /login/
Exception Value: 'unicode' object is not callable

1 个答案:

答案 0 :(得分:1)

在您的网址文件中,您需要删除视图名称周围的字符串。

像这样:

urlpatterns = patterns('',
    url(r'^login/$', LoginRequest), #Shred the quotes
    url(r'^logout/$', LogoutRequest), #Shred the quotes

请注意新的django recommended way is

from core import views

urlpatterns = patterns('',
    url(r'^login/$', views.LoginRequest),
    url(r'^logout/$', views.LogoutRequest),

此外,您可以像这样简化视图:

def LoginRequest(request):

    redirect_to = request.META.get('HTTP_REFERER', '/')
    if request.user.is_authenticated():
        return HttpResponseRedirect(redirect_to)
    form = LoginForm()
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = authenticate(email=email, password=password)
            if user is not None:
                login(request, user)
                return HttpResponseRedirect(redirect_to)
    return render_to_response('/', {'form': form}, context_instance=RequestContext(request))

此外,/必须类似于:/index.html - 取决于模板的位置。