Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: UnboundLocalError at /receptionist/patient_form/ local variable 'username' referenced before assignment

The following code gives the error : UnboundLocalError at /receptionist/patient_form/
local variable ‘username’ referenced before assignment.
Exception Type: UnboundLocalError
Exception Value:
local variable ‘username’ referenced before assignment.

`

@login_required
def createPatient(request):
    form=PatientForm(request.POST, request.FILES)
    # try:
    if request.method == "POST":
            if form.is_valid():
            
                first_name = form.cleaned_data['first_name']
                last_name = form.cleaned_data['last_name']
                username = form.cleaned_data['username']
                email = form.cleaned_data['email']
                password = form.cleaned_data['password']
                address = form.cleaned_data['address']
                phone_number = form.cleaned_data['phone_number']
                dob = form.cleaned_data['dob']
                gender = form.cleaned_data['gender']
            
        
            user = CustomUser.objects.create_user(username=username, email=email,password=password, first_name=first_name, last_name=last_name,user_type=5)
            user.patients.address = address
            user.patients.phone_number = phone_number
            user.patients.dob=dob
            user.patients.gender=gender
            user.save()

How Can I Fix This ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

local variable ‘username’ referenced before assignment error will come, if form.is_valid() returns False because you use username variable for creating user.

@login_required
def createPatient(request):
    form=PatientForm(request.POST, request.FILES)
    # try:
    if request.method == "POST":
        if form.is_valid():
        
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            address = form.cleaned_data['address']
            phone_number = form.cleaned_data['phone_number']
            dob = form.cleaned_data['dob']
            gender = form.cleaned_data['gender']
        
    
            user = CustomUser.objects.create_user(username=username, email=email,password=password, first_name=first_name, last_name=last_name,user_type=5)
            user.patients.address = address
            user.patients.phone_number = phone_number
            user.patients.dob=dob
            user.patients.gender=gender
            user.save()
        else:
            # handle in case of form is not valid, i.e. form have some errors
    else:
        # handle in case of user send a GET request ( like raise error GET method not allowed)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading