Answer the question
In order to leave comments, you need to log in
Django - how to add profile fields on registration?
Good afternoon!
1) There are 2 models (Standard User and Profile)
2) There is a connection through signals - when a user is created, a profile is created associated with it.
3) There is a standard registration form (Login, email, password, password)
Question - how can I add a field for profile to the registration form?
whether it is possible to transfer value of an additional field together with user instance through signals?
signals.py
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
<fieldset class="form-group">
<legend class="border-bootom mb-4">Регистрация</legend>
{{ user_form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Зарегистрироваться</button>
</div>
{{ user_form|crispy }}
<input type="text" id="city" >
<button type="submit">Зарегистрироваться</button>
-------------------------
чтобы значение city как-то передать сюда в сигналах:
Profile.objects.create(user=instance, city = input.city)
Answer the question
In order to leave comments, you need to log in
Why do you need signals here, explicitly create a profile in the place where you process the form
Instead of a separate profile, consider overriding the user model (django custom user model). Then it will be possible to add new fields to the model and to the registration form (which, judging by the documentation, can be overridden).
In principle, form.create() can also create a profile according to its fields - as an alternative to a custom user if you absolutely need users without profiles at all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question