'str' 对象没有属性 'socialaccount_set' django allauth

'str' object has no attribute 'socialaccount_set' django allauth

我正在尝试将基于函数的视图更改为基于 class 的视图

我有这个基于功能的个人资料视图:

@verified_email_required
@login_required
def profile(request, username):
    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()
            message = messages.success(request, f'Your profile has been updated')
            return redirect('profile', username=username)

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)
    try:
        profile = User.objects.get(username=username)
    except User.DoesNotExist:
        message = messages.warning(request,f'Profile not found for {username}')

        return redirect('home')
        profile = ''


    all_post_by_user = Log.objects.filter(author__username=username)
    context = {
        'u_form' : u_form,
        'p_form' : p_form,
        'profile' : profile, 
        'all_post_by_user' : all_post_by_user
    }

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

这是我的 class 基于相同的 :

class ProfileDetailView(DetailView):
    model = Profile
    template_name = "users/profile.html"
    context_object_name = 'profile'

    def get_object(self):
        username = self.kwargs.get('username')
        view_profile = Profile.objects.get(user__username=username)

所以,我收到了这个错误:

profile.html:

{% extends 'log/base.html' %}
{% block content %}
{% load socialaccount %}
{% get_social_accounts profile as accounts %}
{%load crispy_forms_tags %} 
<title>Error logger - Profile {{ profile.username }}</title>

<div id='profile' class="content-section card  p-4">
  <div class="media">
{% if profile.username == user.username %}

  {% if accounts %} 
  <img class='rounded-circle account-img' src="{{ profile.socialaccount_set.all.0.get_avatar_url }}" />
  {% else %}
      <img class="rounded-circle account-img" src="{{ profile.profile.avatar.url }}">
  {% endif %}


  {% else %}

  {% if accounts %}
  
  <img class='rounded-circle account-img' src="{{ profile.socialaccount_set.all.0.get_avatar_url }}" />

  {% else %}
  
      <img class="rounded-circle account-img" src="{{ profile.profile.avatar.url }}">
  {% endif %}

{% endif %}

    <div class="media-body">
      <h2 class="account-heading">{{profile.username}}</h2>
      <p >{{profile.email}}</p>
      <p>Created on: {{ profile.profile.created }}</p>
      {% if profile.username == user.username %}

      <p>Last updated on : {{ profile.profile.updated }}</p>
    {% endif %}
    </div>
  </div>



  <!-- FORM HERE -->

{% if profile.username == user.username %}


  <form method='POST' autocomplete="off" enctype="multipart/form-data" >
    {% csrf_token %}

    <fieldset class='form-group'>
        <legend class='border-bottom mb-4'>Update Profile</legend>
        {{ u_form | crispy }}
        {{ p_form | crispy }}
    </fieldset>

    <div class='form-group'>
        
        <button class='btn btn-outline-info' type='submit'>Update</button>

    </div>

  </form>

    {% endif %}
  

<div class="container border-top mt-4 pt-4">

  <legend>Posts</legend>
  
  {% for i in all_post_by_user %}


  <div id="you-want-lazyload" data-lazyload="&lt;p&gt;Anything you want to lazyload&lt;/p&gt;" class='m-4'>
    <div class="container main m-4" style="width: 50vw;">
        <a class='link' href="{% url 'log-detail' i.slug   %}"><h2 >{{ i.title }}</h2></a>
        <p>{{ i.content }}</p>
        <p class='small'>{{ i.created }}</p>
    </div>

  </div>


  {% endfor %}

</div>

</div>
{% endblock content %}

如何解决我遇到的错误?

我正在使用 django allauth 进行社交登录 google

也有人可以解释为什么我必须查询它是 user__username=username 吗?

谢谢

好的,所以错误告诉我们您在某个地方得到了一个您不期望的字符串,因为您正在尝试访问由 allauth 提供的变量的属性。

发生这种情况是因为您在此处进行了异常处理;


    try:
        profile = User.objects.get(username=username)
    except User.DoesNotExist:
        message = messages.warning(request, f'Profile not found for {username}')

        return redirect('home')
        profile = ''

如果用户不存在,您将 profile 设置为一个空字符串并在上下文中传递。

所以我会稍微改变一下,以修复此错误并避免查找 User 对象并将它们称为 profile 的混淆(我假设 User 被称为 user 或类似)


    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        message = messages.warning(request, f'User not found for {username}')

        return redirect('home')
        user = None


    all_post_by_user = Log.objects.filter(author__username=username)
    context = {
        'u_form' : u_form,
        'p_form' : p_form,
        'user' : user, 
        'all_post_by_user' : all_post_by_user
    }
    
    return render(request, 'users/profile.html', context)

profile.html:

{% extends 'log/base.html' %}
{% load crispy_forms_tags socialaccount %}

{% block content %}
    {% get_social_accounts profile as accounts %}
    <title>Error logger - Profile {{ profile.username }}</title>
    
    <div id='profile' class="content-section card  p-4">
      <div class="media">

    {% if profile and profile.username == user.username %}
    

      {% if accounts %} 
          <img class='rounded-circle account-img' src="{{ profile.socialaccount_set.all.0.get_avatar_url }}" />
      {% else %}
          <img class="rounded-circle account-img" src="{{ profile.profile.avatar.url }}">
      {% endif %}
    
    {% else %}

      {% if accounts and profile %}
      
          <img class='rounded-circle account-img' src="{{ profile.socialaccount_set.all.0.get_avatar_url }}" />
    
      {% elif profile %}
      
          <img class="rounded-circle account-img" src="{{ profile.profile.avatar.url }}">
      {% endif %}
    
    {% endif %}
    
    {% if profile %}
        <div class="media-body">
          <h2 class="account-heading">{{profile.username}}</h2>
          <p >{{profile.email}}</p>
          <p>Created on: {{ profile.profile.created }}</p>
          {% if profile.username == user.username %}
    
          <p>Last updated on : {{ profile.profile.updated }}</p>
        {% endif %}
        </div>
      </div>
    
    
      <!-- FORM HERE -->
    
      {% if profile.username == user.username %}
    

      <form method='POST' autocomplete="off" enctype="multipart/form-data" >
        {% csrf_token %}
    
        <fieldset class='form-group'>
            <legend class='border-bottom mb-4'>Update Profile</legend>
            {{ u_form | crispy }}
            {{ p_form | crispy }}
        </fieldset>
    
        <div class='form-group'>
            
            <button class='btn btn-outline-info' type='submit'>Update</button>
    
        </div>
    
      </form>
    
      {% endif %} <!-- end username check -->
    {% endif %} <!-- end profile check -->
    
    <div class="container border-top mt-4 pt-4">
    
      <legend>Posts</legend>
      
      {% for i in all_post_by_user %}
    
    
      <div id="you-want-lazyload" data-lazyload="&lt;p&gt;Anything you want to lazyload&lt;/p&gt;" class='m-4'>
        <div class="container main m-4" style="width: 50vw;">
            <a class='link' href="{% url 'log-detail' i.slug   %}"><h2 >{{ i.title }}</h2></a>
            <p>{{ i.content }}</p>
            <p class='small'>{{ i.created }}</p>
        </div>
    
      </div>
    
    
      {% endfor %}
    
    </div>
   
    </div>
    {% endblock content %}