Django:'NoneType'对象没有属性'user',虽然已定义

时间:2017-07-21 15:07:11

标签: python django request nonetype

我是django的新手,并尝试使用此代码获取图片网址并将其保存到数据库中:

@login_required
def image_create(request):
if request.method == 'POST':
    #form is sent
    form = ImageCreateForm(data=request.POST)
    if form.is_valid():
        #form data is valid
        cd = form.cleaned_data
        new_item = form.save(commit=False)

        #assign current user to the item:
        new_item.user = request.user
        new_item.save()
        messages.success(request, 'image added successfully')

        #redirect to the new created item detail view:
        return redirect(new_item.get_absolute_url())
else:
    #build the form with the data provided by bookmarklet via GET:
    form = ImageCreateForm(data=request.GET)

return render(request, 'images/image/create.html', {'section': 'images', 'form': form})

但是当我尝试使用此网址添加图片时:

  

本地主机/图像/创建/标题= IMAGE_NAME&安培;?URL = SOME_URL

我收到以下错误:

AttributeError at /images/create/
'NoneType' object has no attribute 'user'

并指向视图的第17行,即:

new_item.user = request.user

我很感激你的帮助!

0 个答案:

没有答案