1

I'm trying to render two forms (signup and login) on the same page in Django. I want to POST the data to either field by changing the <form action="some_url" tag. It's the first suggestion in this answer: https://stackoverflow.com/a/1395866/2532070

Is this possible? Currently I have:

urls.py:

url(r'^', HomePageView.as_view()),
url(r'^signup/$', SignupUser.as_view(), name="signup_user"),

views.py:

class HomePageView(View):
    template_name = "index.html"

    login_form = AuthenticationForm()
    signup_form = UserCreationForm()

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, {
            'login_form': self.login_form, 
            'signup_form': self.signup_form,
            'signup_action': reverse("signup_user")} 
        )

class SignupUser(CreateView):
    model = settings.AUTH_USER_MODEL
    form_class = UserCreationForm

my template:

<form action="{{ signup_action }}" method="POST">{% csrf_token %}
    {{ signup_form }}
    <input type="submit" value="Signup">
</form>

Unfortunately, the POST seems to be calling the post function of HomePageView, rather than calling the SignupUser view.

Is it possible to call my SignupUser view with the POST data by correctly formatting my form's action="..." tags?

Community
  • 1
  • 1
YPCrumble
  • 20,703
  • 15
  • 86
  • 149

1 Answers1

1

The problem is not where you think it is.

The issue is that your initial URL, which points at HomePageView, catches everything, because you do not specify the end of the string. You have a start anchor, ^, but not an end anchor, which means it basically matches "every string that starts", ie everything.

You need to only match the empty string:

url(r'^$', HomePageView.as_view()),
Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786