请求对象没有用户Django

Rav*_*hli 2 django django-templates django-views

我正在制作一个钱包应用程序,所以如果有人去http://127.0.0.1:8000/add_money/加钱,他们按提交,钱必须添加到他们的钱包但提交后有一个错误:

AttributeError at /add_money/ 'unicode' object has no attribute 'user'
Run Code Online (Sandbox Code Playgroud)

为了更好地理解操作流程,我提出了一些检查点:

Request <WSGIRequest: GET '/add_money/'>
Request  j <WSGIRequest: GET '/add_money/'>
[15/Dec/2016 15:26:34] "GET /add_money/ HTTP/1.1" 200 420
Request <WSGIRequest: POST '/add_money/'>
Request 3
[15/Dec/2016 15:26:37] "POST /add_money/ HTTP/1.1" 500 66535
Run Code Online (Sandbox Code Playgroud)

add_money 视图

def add_money(request):
    print ("Request %s" % request)
    if request.user:
        if request.POST and request.POST.get('amount'):
            username = request.user.username
            add_amount = request.POST.get('amount')
            wallet = Wallet.objects.filter(username=username).update(add_money(add_amount))
            now = datetime.now()
            trans = Transaction(from_name=username, wallet_id=wallet.id, date=now, amount=add_amount)
            trans.save()
            print ("Request s %s" % request)
            return render(request, 'user_profile.html', {'user': request.user})
        else:
            print ("Request  j %s" % request)
            return render(request, 'add_money.html')
    else:
        print ("Request rf %s" % request)
        return HttpResponseRedirect('/login/?next={}'.format('/add_money/'))
Run Code Online (Sandbox Code Playgroud)

// add_money模板

<form method="post">
    Amount:<input type="number" name="amount">
    <input type="submit" value="Submit">
    <button type="button" name="cancel">Cancel</button>
    {% csrf_token %}
</form>
Run Code Online (Sandbox Code Playgroud)

编辑1追溯:

File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/ravinkohli/PycharmProjects/untitled1/wallet/views.py" in add_money
  18.             wallet = Wallet.objects.filter(username=username).update(add_money(add_amount))
File "/Users/ravinkohli/PycharmProjects/untitled1/wallet/views.py" in add_money
  14.     if request.user:

Exception Type: AttributeError at /add_money/
Exception Value: 'unicode' object has no attribute 'user'
Run Code Online (Sandbox Code Playgroud)

编辑2 //urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('django.contrib.auth.urls')),
    url(r'^create-user/$', views.create_user),
    url(r'^accounts/profile/$', user_profile),
    url(r'^add_money/$', add_money),
    url(r'subtract-money/$', subtract_money)
]
Run Code Online (Sandbox Code Playgroud)

我注意到第三个请求是unicode但我不知道为什么?

e4c*_*4c5 5

假设中间件错误的原始答案

这是因为您尚未启用AuthenticationMiddleware

将表示当前登录用户的用户属性添加到每个传入的HttpRequest对象.请参阅Web请求中的身份验证

你的settings.py应该有这样的东西

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
...
)
Run Code Online (Sandbox Code Playgroud)

基于堆栈跟踪更新的答案

你在这里调用django视图作为一个普通的python函数.add_amount显然不是一个HttpRequest实例.

 Wallet.objects.filter(username=username).update(add_money(add_amount))
Run Code Online (Sandbox Code Playgroud)

事实上,这整个陈述没有意义.在django,标准形式是

.update(field_name=new_value)
Run Code Online (Sandbox Code Playgroud)

但是你传递函数调用的响应来更新.你在想什么?

wallet = Wallet.objects.filter(username=username).update(amount = add_money)
Run Code Online (Sandbox Code Playgroud)

或者您是否想过在现有价值上添加一个数字?然后你将不得不使用django F表达式.

 wallet = Wallet.objects.filter(username=username).update(amount = F('amount') + add_money)
Run Code Online (Sandbox Code Playgroud)