ACA
ACA

Reputation: 47

Passing multiple arguments in django view

I have tree different users and I want to use one view function and three different forms. So I need to pass two arguments to user_signup view: the request object and the form Class. In urls.py I have the following code

path('signup/admin/', views.user_signup(request, AdminSignupForm), name='admin_signup')

in views.py I defined user_signup function

def user_signup(request, form, template_name='users/signup_staff.html'):
    if request.method == 'POST':
        form = form(request.POST)
        if form.is_valid():
            user = form.save()  # save function is redefined in AdminSignupForm
    else:
        form = form()
    return render(request, template_name, {'form': form})

How can I pass a request object to user_signup?

Upvotes: 0

Views: 2346

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

Use the third argument to the path function.

path('signup/admin/', views.user_signup, {'form': AdminSignupForm}, name='admin_signup')

Upvotes: 1

Related Questions