简体   繁体   English

/profile/ 'function' 对象的 AttributeError 没有属性 'object

[英]AttributeError at /profile/ 'function' object has no attribute 'object

here is my views.py file but i keep getting this error, what could be the issue here.这是我的 views.py 文件,但我不断收到此错误,这可能是什么问题。 Profile is a class in the models.py code. Profile 是models.py 代码中的一个类。 if you need another part of my code please ask如果您需要我的代码的另一部分,请询问

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm

def register(request):
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            messages.success(request, f"Your account has been created! You are now able to login!")
            return redirect("login")
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

@login_required
def Profile(request):
    profile.objects.get_or_create(user=request.user)
    if request.method == "POST":
        u_form = UserUpdateForm( request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f"Your account has been updated!")
            return redirect("profile")
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.Profile)

    context = {
        'u_form': u_form,
        'p_form': p_form,


    }
    return render(request, "users/profile.html", context)

AttributeError at /profile/'function' object has no attribute 'objects' Request Method: GET Request URL: http://127.0.0.1:8000/profile/ Django Version: 2.2.8 Exception Type: AttributeError Exception Value:'function' object has no attribute 'objects' Exception Location: /Users/ /Desktop/project/users/views.py in profile, line 20 Python Executable: /Users/ /.local/share/virtualenvs/project-9FFMjpiO/bin/python /profile/'function' 对象的 AttributeError 没有属性 'objects' 请求方法:GET 请求 URL: http : //127.0.0.1 :8000/profile/ Django 版本:2.2.8 异常类型:AttributeError 异常值:'function'对象没有属性“对象”异常位置:/Users/ /Desktop/project/users/views.py in profile, line 20 Python Executable: /Users/ /.local/share/virtualenvs/project-9FFMjpiO/bin/python

Line number first of Function Profile , the model Profile is probably not imported OR if imported using profile , Function Profile第一个行号,模型Profile可能未导入,或者如果使用profile导入,

profile.objects.get_or_create(user=request.user)

means you are referring to profile function not the Model itself.意味着您指的是profile功能而不是模型本身。

So replace profile with Profile and also import the model in your views.py.因此,更换profileProfile ,并导入模型在views.py。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM