【发布时间】: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="<p>Anything you want to lazyload</p>" 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。我怀疑你有Profile和User的 FK 来扩展模型,这也解释了使用user__username的必要性,因为你会遵循这种关系。
标签: django django-views django-templates django-class-based-views