Nicolas
Nicolas

Reputation: 444

Django: UserCreationForm not showing

I'm trying to create a user sign-up form. It largely works, except that the form does not show in the template. My base template renders, just not the form itself. I get no traceback errors, the page (aside from the "base.html" template) is blank i.e. white space, with not even a button.

I'm trying to recreate the tutorial done here: https://wsvincent.com/django-user-authentication-tutorial-signup/

signup.html

{% extends 'base.html' %}

{% block title %}Sign Up{% endblock %}

{% block content %}
  <h2>Sign up</h2>
  <form method="post">
    {% csrf_token %}
    {{ form.p }}
    <button type="submit">Sign up</button>
  </form>
{% endblock %}

accounts/urls.py

from django.urls import path
from . import views

app_name = 'accounts'

urlpatterns = [
     path('signup/', views.SignUp.as_view(), name='signup'),
]

myproject/urls.py

from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from clincher import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('clincher/', include('clincher.urls'), name='clincher'),
    path('accounts/', include('accounts.urls')),
    path('accounts/', include('django.contrib.auth.urls'), name='accounts'),
    path('', RedirectView.as_view(url='clincher/templates/')),
]

views.py

from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic

class SignUp(generic.CreateView):
    form_class = UserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

Upvotes: 0

Views: 2081

Answers (1)

Krushi Raj
Krushi Raj

Reputation: 538

There is a problem in your template.

<form method="post">
    {% csrf_token %}
    {{ form.p }}
    <button type="submit">Sign up</button>
</form>

Change your code in template from above to the following one:

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Sign up</button>
</form>

When you give it as form.p the template engine searches for an attribute with name 'p' inside 'form' object, but that is not what we want. So use form.as_p instead to render the form wrapped in paragraph elements.

Upvotes: 2

Related Questions