Python Django-用户已创建,但缺少配置文件

Python Django-用户已创建,但缺少配置文件,python,django,user-profile,Python,Django,User Profile,当我注册一个新用户时,我可以从管理页面看到该用户是被创建的。 但是每当我在导航栏中打开我的个人资料页面时,我都会得到以下错误 在我在我的个人资料页面上添加一个函数(一个删除用户和个人资料的按钮)之前,它实际上是工作的,但是我不知道我需要在页面上添加什么来修复这个错误 profile.html {% extends "digitalFarm/base.html" %} {% load crispy_forms_tags %} {% block content %}

当我注册一个新用户时,我可以从管理页面看到该用户是被创建的。 但是每当我在导航栏中打开我的个人资料页面时,我都会得到以下错误

在我在我的个人资料页面上添加一个函数(一个删除用户和个人资料的按钮)之前,它实际上是工作的,但是我不知道我需要在页面上添加什么来修复这个错误

profile.html

{% extends "digitalFarm/base.html" %}
{% load crispy_forms_tags %}

{% block content %}
      <div class="content-section">
             <div class="media">
                    <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
                    <div class="media-body">
                           <h2 class="account-heading">{{ user.username }}</h2>
                           <p class="text-secondary">{{ user.email }}</p>
                    </div>
             </div>
          <form method="POST" enctype="multipart/form-data">
              {% csrf_token %}
              <fieldset class="form-group">
                  <legend class="boder-bottom mb-4">Profile info</legend>
                  {{ u_form|crispy }}
                  {{ p_form|crispy }}
                  {{ d_form|crispy }}
              </fieldset>
              <div class="form-group">
                  <button class="btn btn-outline-info" type="submit">Update Profile</button>
              </div>
          </form>

          <form>
              {% csrf_token %}
              <fieldset class="form-group">
                  <legend class="border-bottom mb-4">Delete User Account</legend>
              </fieldset>
              <div class="form-group">
                  <button class="btn btn-danger">
                      <a style="color: white" href="{% url 'profile_confirm_delete' %}">Delete Account</a></button>
              </div>
          </form>
      </div>
{% endblock content %}
signals.py

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    print("INSIDE CREATE_PROFILE")#negative
    if created:
        print("INSIDE CREATED_PROFILE")#negative
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs): #kwargs= accepts any additional keywords
        instance.profile.save()

@receiver(pre_delete, sender=User)
def delete_profile(sender, instance, **kwargs): #kwargs= accepts any additional keywords
        instance.profile.delete()
forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

#Update
class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email']

class ProfileUpdateForm(forms.ModelForm):
    print("INSIDE ProfileUpdateForm")
    class Meta:
        model = Profile
        fields = ['image']

#Delete
class ProfileDeleteForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

class UserDeleteForm(forms.ModelForm):
    class Meta:
        model = User
        fields = []   #Form has only submit button.  Empty "fields" list still necessary, though.

在你的signals.py中,我没有看到任何save()指令,你的配置文件实例应该如何进入DB,DB profile表是空的。您需要这样的内容(用于创建):

或者实际上

profileInstance = Profile.objects.create(user=instance)
profileInstance.initNewProfile() # setup context default values
profileInstance.save()

此外,在删除时,实际上有更好的选项来删除依赖实例:ORM级别的级联。必须在类(models.py)

文档中定义
.objects。create
是一个包含
.save
方法的快捷方式。您不需要在
.create
之后手动调用
.save
。这是个好主意!我两种方法都试过了,但不幸的是没有成功。。。但我也假设信号中的create_profile方法甚至不会被输入,因为我的终端没有打印任何内容(1)它必须打印degug注释,否则它不会触发。如果删除一个实例,则必须在某个点创建显式的新实例。它在哪里。创建新用户时,用户创建事件仅触发一次。如果配置文件已删除,则您的instance.profile.save()没有要保存的实例。但在创建用户时,甚至没有创建的配置文件。我没有删除任何个人资料。但是,每当我登录并点击我的个人资料页面时,我都会看到这个错误。在你的个人资料视图中,我没有看到任何地方声明了
d_form
{{d_form | crispy}
)。这是从哪里来的?我也注意到了,所以我最近删除了它。它应该用于删除配置文件。我想也许我可以创建,但也可以在创建之后删除它。但即使在我删除了d_u表格之后,这些档案仍然不见了。
Profile.objects.create(user=instance).save()
profileInstance = Profile.objects.create(user=instance)
profileInstance.initNewProfile() # setup context default values
profileInstance.save()