简体   繁体   中英

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. Profile is a class in the models.py code. 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

Line number first of Function Profile , the model Profile is probably not imported OR if imported using profile ,

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

means you are referring to profile function not the Model itself.

So replace profile with Profile and also import the model in your views.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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