Django信号无法在usercreation上运行 [英] Django signals not working on usercreation
本文介绍了Django信号无法在usercreation上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 signals.py
文件中创建了一个信号,该信号应该为每个创建的用户创建一个配置文件,但是它不起作用.我试图检查命令 python manage.py check
是否存在错误,但这似乎也不起作用.
I created a signals supposed to create a profile for each created users in my signals.py
file but it's not working. I've tried to check if there's an error with the command python manage.py check
, but that seems not to work as well.
models.py
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f"{self.user.username}'s profile "
signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, created, instance, **kwargs):
if created:
Profle.instance.create(user=instance)
@receiver(post_save, sender=User)
def create_profile(sender, instance, **kwargs):
instance.profile.save()
apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
我在做什么错了?
推荐答案
问题出在我如何将用户应用安装在主项目目录的settings.py文件中.
The problem is from how i installed the users app in the settings.py file in the main project directory.
我用过:
INSTALLED_APPS = [
'users',
]
**而不是**
INSTALLED_APPS = [
'users.apps.UsersConfig',
]
这篇关于Django信号无法在usercreation上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文