I
I
inspekd2020-08-29 14:55:58
Django
inspekd, 2020-08-29 14:55:58

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()


register.html
<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>


I want to get something like

Pseudocode:
{{ user_form|crispy }}
 <input type="text" id="city" >
<button type="submit">Зарегистрироваться</button>
-------------------------
чтобы значение city как-то передать сюда в сигналах: 
   Profile.objects.create(user=instance, city = input.city)


Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-08-29
@bacon

Why do you need signals here, explicitly create a profile in the place where you process the form

S
Sergey Tikhonov, 2020-09-09
@tumbler

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 question

Ask a Question

731 491 924 answers to any question