django - Profile() 得到了一个意外的关键字参数 'username'

django - Profile() got an unexpected keyword argument 'username'

当用户访问网站 url / 用户名时,我试图让 http://127.0.0.1:8000/destiny/ 但它抛出 Profile() 得到了一个意外的关键字参数 'username' 错误。我尝试在我的个人资料视图中添加用户名参数。我还尝试在用户注册时获取用户名,以便在个人资料视图中使用它,但它仍然不起作用。我是 Django 新手,这就是为什么我真的不知道错误来自哪里

更新:另一个错误现已修复。但是当我想查看其他人的个人资料时,它总是return我自己的个人资料

views.py

def UserProfile(request, username):
    user = get_object_or_404(User, username=username)
    profile = Profile.objects.get(user=user)
    url_name = resolve(request.path).url_name
    
    context = {
        'profile':profile,
        'url_name':url_name,
    }

    return render(request, 'userauths/profile.html', context)




#register view
def register(request):
    reviews = Review.objects.filter(status='published')
    info = Announcements.objects.filter(active=True)
    categories = Category.objects.all()
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            username = form.cleaned_data.get('username')

            messages.success(request, f'Hurray your account was created!!')
            new_user = authenticate(username=form.cleaned_data['username'],
                                    password=form.cleaned_data['password1'],
                                    )
            login(request, new_user)
            return redirect('elements:home')
            


    elif request.user.is_authenticated:
        return redirect('elements:home')
    else:
        form = UserRegisterForm()
    context = {
        'reviews': reviews,
        'form': form,
        'info': info,
        'categories': categories
    }
    return render(request, 'userauths/register.html', context)

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
    first_name = models.CharField(max_length=1000, null=True, blank=True)
    last_name = models.CharField(max_length=1000, null=True, blank=True)
    email = models.EmailField(null=True, blank=True)
    phone = models.IntegerField(null=True, blank=True)
    bio = models.CharField(max_length=1000, null=True, blank=True)
    aboutme = models.TextField(null=True, blank=True, verbose_name="About Me")
    country = models.CharField(max_length=1000, null=True, blank=True)
    cover_photo = models.ImageField(default='default.jpg', upload_to="profile_pic")
    image = models.ImageField(default='default.jpg', upload_to="profile_pic")
    joined = models.DateTimeField(auto_now_add=True, null=True)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

    def __str__(self):
        return f'{self.user.username} - Profile'

主要项目urls.py

from userauths.views import Profile

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('userauths.urls')),
    path('<username>/', Profile, name='profile'),

更新:第一个错误已修复。这是因为我的个人资料名称是 UserProfile,而我使用的是 Profile。

现在我得到这个错误 'function' object has no attribute 'objects'

完整追溯

System check identified no issues (0 silenced).
January 03, 2022 - 13:16:50
Django version 3.1, using settings 'dexxapikprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /destiny/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
    profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:16:51] "GET /destiny/ HTTP/1.1" 500 66676
[03/Jan/2022 13:16:52] "GET /favicon.ico HTTP/1.1" 301 0
Not Found: /favicon.ico/
[03/Jan/2022 13:16:52] "GET /favicon.ico/ HTTP/1.1" 404 1736
Internal Server Error: /destiny/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
    profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:16:53] "GET /destiny/ HTTP/1.1" 500 66676
Internal Server Error: /destiny/
Traceback (most recent call last):
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\Desktop\dexxapik3\DexxaPik\dexxapikprj\userauths\views.py", line 21, in Profile
    profile = Profile.objects.get(user=user)
AttributeError: 'function' object has no attribute 'objects'
[03/Jan/2022 13:17:02] "GET /destiny/ HTTP/1.1" 500 66676

你得到了那个错误'因为视图被称为 UserProfile 而不是 Profile