在基于函数的视图中引用模型的问题

时间:2019-01-27 21:52:43

标签: django django-models django-views

如标题所述,我在cmsUserDetailEdit视图中引用我的帐户模型时遇到麻烦。该站点本身并没有损坏,但似乎无法找到模型,因为引用模型的所有位置均为空。

我尝试将帐户放在上下文中。例如:'account': account,但没有运气。

views.py

def cmsUserDetailEdit(request, pk):
    account = Account.objects.all()
    accounts = Account.objects.get(pk=pk)

    if request.method == 'POST':
        u_form = UserEditPersonalForm(request.POST, instance=accounts.user)
        a_form = AccountEditPersonalForm(request.POST, instance=accounts)

        if u_form.is_valid() and a_form.is_valid():
            u_form.save()
            a_form.save()
            messages.success(request, f'Account updated')
            return redirect('/cms/test/')
    else:
        u_form = UserEditPersonalForm(instance=accounts.user)
        a_form = AccountEditPersonalForm(instance=accounts)

    context = {
        'u_form': u_form,
        'a_form': a_form,
    }    

    return render(request, 'cms/users/cms-users-user-edit.html', context)

cms-users-user-edit.html

<div class="note-card-content">
    <div class="content">
        {{ account.note }} <!-- This part is empty -->
     </div>
</div> 

0 个答案:

没有答案