简体   繁体   中英

Django Attach variables from one step to another

How would I grab the 2nd form and add it to the first form then selectively not allow that user to login. In the Doctorwizard done function.

maybe add a variable status?

etc.

username,password,email,first_name,last_name,verified

views.py

from django.core.files.storage import FileSystemStorage
import os
from django.conf import settings
class DoctorWizard(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'doctor'))
    template_name = "registration/signup.html"
    form_list = [SignUpForm,verify]
    
    def done(self, form_list, **kwargs):
        data=process_data(form_list)
        return redirect('home')

forms.py

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

    class Meta:
        model = Profile
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )

class verify(forms.Form):
    verified = forms.ImageField(required=True)
    class Meta:
        model = Profile
        fields = ('verified',)

models.py

class Profile(AbstractUser):
    
    bio = models.TextField(max_length=100, blank=True)
    phone_number = PhoneNumberField(max_length=25, region="US")
    birth_date = models.DateField(blank = True, null = True) 
    is_doctor = models.BooleanField(default=False)
    verified = models.ImageField(upload_to='media/doctor')
    date_created = models.DateTimeField(auto_now_add=True)
    avatar = models.ImageField(default='default.png', upload_to='')
def done(self, form_list, **kwargs):
        process_data(form_list)
        userCreate = form_list[0]
        userCreate.save()
        username = userCreate.cleaned_data.get('username')
        raw_password = userCreate.cleaned_data.get('password1')
        user = authenticate(username=username, password=raw_password)
        if user:
            user.verified=form_list[1].cleaned_data.get('verified')
            user.is_doctor=True
            user.is_active=False
            user.save()

Just grab the user and access it's fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM