how to give a user a model field to choose from during signup?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
AJD
Reply | Threaded
Open this post in threaded view
|

how to give a user a model field to choose from during signup?

AJD

how to make a user registration form including a model field in it as a required field?


like i want 

college

field to show up on registration page as a drop-down menu


my models.py


class Profile(models.Model):
    user
= models.OneToOneField(User, on_delete=models.CASCADE)
    college
= models.ForeignKey(College, on_delete=models.CASCADE)


@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
   
if created:
       
Profile.objects.create(user=instance)
    instance
.profile.save()


class College(models.Model):
    col
= models.CharField(max_length=100)

   
def __str__(self):
       
return self.col




my forms.py


class UserForm(UserCreationForm):
    col
= College.objects.all()
    password1
= forms.CharField(widget=forms.PasswordInput)
    password2
= forms.CharField(widget=forms.PasswordInput)
    college
= forms.ChoiceField(widget=forms.Select(choices=col))

class Meta:
    model
= User
    fields
= ('username', 'college', 'password1', 'password2',)


and the views.py includes this:


def signup(request):
if request.method == 'POST':
    form
= SignUpForm(request.POST)
   
if form.is_valid():
        user
= form.save()
        user
.refresh_from_db()
        user
.save()
        raw_password
= form.cleaned_data.get('password1')
        user
= authenticate(username=user.username, password=raw_password)
        login
(request, user)
       
return redirect('home')
else:
    form
= SignUpForm()
return render(request, 'registration_form.html', {'form': form})


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1f92349a-35f3-47f2-99e2-612252498718%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.