Django same function data not showing after page render

I am creating university management system using django. I have created faculty(teacher) registration form. For that, in my views.py

def faculty_registration(request):
    data = {}
    form = FacultyRegisterForm()
    activetab = 'list'
    if request.method == 'POST':
        
        activetab = 'add'
        form = FacultyRegisterForm(request.POST)
        if form.is_valid():
            userdata = User()

            if User.objects.filter(username=request.POST.get('email')).exists():
                messages.error(request, f"This email already exists.")
                return redirect('/faculty/faculty_registration')

            else:
                userdatafirst_name = request.POST.get("first_name")
                userdata.username = request.POST.get('email')
                userdata.email = request.POST.get('email')
                try:  
                    fac_ID = Faculty.objects.last().id         
                except:
                    fac_ID = 0 
                LastInsertId = fac_ID+1
                print('after_id',LastInsertId)
                password = User.objects.make_random_password()
                faculty_pw = password+str(LastInsertId)
                print("pass",faculty_pw)
                userdata.password = make_password(faculty_pw)
                print( "teacher_pasword",userdata.password)
                userdata.save()

                fac = Faculty()
                fac.faculty_college_id = request.POST.get("faculty_college")
                fac.faculty_programme_id = request.POST.get('faculty_programme')
                fac.salutation = request.POST.get("salutation")
                fac.first_name = request.POST.get("first_name")
                fac.middle_name = request.POST.get("middle_name")
                fac.last_name = request.POST.get("last_name")
                fac.phone = request.POST.get("phone")
                fac.email = request.POST.get("email")
                fac.address = request.POST.get('address')
                fac.department = request.POST.get('department')
                fac.highest_qualification = request.POST.get("highest_qualification")
                fac.years_of_experience = request.POST.get('years_of_experience')
                fac.previous_institute = request.POST.get('previous_institute')
                # fac.documents = request.POST.get("documents")

                if 'documents' in request.FILES:

                    filename = request.FILES['documents']
                    if str(filename).lower().split('.')[-1] == 'pdf':
                        if int(len(request.FILES['documents'].read())) < (3 * 1024 * 1024):
                            fac.documents = request.FILES['documents']
                        else:
                            messages.warning(request, "please upload a pdf within 3 mb")
                            navshow = 'add'
                            return redirect('faculty_registration')

                    else:
                        messages.warning(request, "please upload a pdf")
                        navshow = 'add'
                        return redirect('faculty_registration')

                fac.photo = request.FILES.get('photo', 'faculty_picture/def.png')
                fac.created_by_id = request.user.id
                fac.user_id = userdata.id
                fac.save()

                assign_role(userdata, 'teacher')           
                
                html_content = render_to_string("email_template.html",{'title':'Kindly Note your Mailid and Password For Login.','to_mail':fac.email,'to_password':faculty_pw})
        
                text_content = strip_tags(html_content)
        
                msg = EmailMessage(
                    'Welcome to University',         #subject
                    text_content,                    #content
                    settings.EMAIL_HOST_USER,        #from email
                    [fac.email],                     #reclist

                )
                
                msg.content_subtype = "html"  # Main content is now text/html
                msg.send()
                # print("send_email",send_email)
                messages.success(request, f"Lecturer has been added successfully")
                return redirect('/faculty/faculty_registration')

        else:
            print("\ninvalid form\n")
    else:
        form = FacultyRegisterForm()

    # fac_data = Faculty.objects.order_by('-id')
    fac_data = Faculty.objects.order_by(Lower('first_name'))
    data['form'] = form
    data['faculties'] = fac_data
    data['activetab'] = activetab

    return render(request, 'faculty_register.html', data)

Also I have added searchbar button to search faculty name. So I created following function:

def searchbar (request):
    data = {}
    find = request.GET['find']
    res = Faculty.objects.filter(Q(first_name__icontains=find) | Q(middle_name__icontains=find)| Q(last_name__icontains=find))
    activetab = 'list'
    form = FacultyRegisterForm()
    data['form'] = form
    data['activetab'] = activetab
    data['faculties'] = res
    return render(request,"faculty_register.html",data)

I have also added 'add' functionality to add faculty. Now my problem is when I click search button, I am not able to add faculty because I think i am rendering the same page 'faculty_register.html' in both return function. So how can I get same page data even after clicking 'search button'.

URLs:

urlpatterns = [
     path('faculty_registration', views.faculty_registration, name='faculty_registration'),
     path('faculty/searchbar', views.searchbar, name= 'searchbarf')]

enter image description here

I've tried to add searchbar function in faculty_registration but not able to solve this.

Back to Top