【问题标题】:'str' object has no attribute 'socialaccount_set' django allauth“str”对象没有属性“socialaccount_set”django allauth
【发布时间】:2021-09-04 21:51:06
【问题描述】:

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

我将此配置文件视图作为基于函数的:

@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 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吗?

谢谢

【问题讨论】:

  • 我对您的用户模型感到困惑。您的原始视图使用User,但您的CBV 使用Profile。我怀疑你有 ProfileUser 的 FK 来扩展模型,这也解释了使用 user__username 的必要性,因为你会遵循这种关系。

标签: django django-views django-templates django-class-based-views


【解决方案1】:

好的,所以该错误告诉我们,您在不希望出现的地方得到了一个字符串,因为您正在尝试访问由 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 被称为@987654326 @ 或类似)


    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 %}

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2021-11-12
    • 2020-07-03
    相关资源
    最近更新 更多