Lev Coe
Lev Coe

Reputation: 11

Django | can't see form {{form.username}}

I can't render form username on the template. like I want to to render the form fields automatically which I defined in forms.py file but it's not working. here you go below for my detailed files forms.py

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

views.py

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm

My views

from .forms import CreateUserForm

Registration

def regPage(request): form = CreateUserForm()

if request.method == 'POST':
    form = CreateUserForm(request.POST)
    if form.is_valid():
        form.save()

context = {'form':form}
return render(request, 'web/car-finder-home.html')

html

<form class="needs-validation" method="POST" action="">
                            {% csrf_token %}
                              <div class="mb-4">
                                  <label class="form-label text-light" for="signup-name">Username</label>
                                  {{ form.username }}
                              </div>
                              <div class="mb-4">
                                  <label class="form-label text-light" for="signup-email">Email address</label>
                                  <input class="form-control form-control-light" type="email" id="signup-email" placeholder="Enter your email" required />
                              </div>
                              <div class="mb-4">
                                  <label class="form-label text-light" for="signup-password">Password <span class="fs-sm opacity-50">min. 8 char</span></label>
                                  <div class="password-toggle">
                                      <input class="form-control form-control-light" type="password" id="signup-password" minlength="8" required />
                                      <label class="password-toggle-btn" aria-label="Show/hide password"> <input class="password-toggle-check" type="checkbox" /><span class="password-toggle-indicator"></span> </label>
                                  </div>
                              </div>
                              <div class="mb-4">
                                  <label class="form-label text-light" for="signup-password-confirm">Confirm password</label>
                                  <div class="password-toggle">
                                      <input class="form-control form-control-light" type="password" id="signup-password-confirm" minlength="8" required />
                                      <label class="password-toggle-btn" aria-label="Show/hide password"> <input class="password-toggle-check" type="checkbox" /><span class="password-toggle-indicator"></span> </label>
                                  </div>
                              </div>
                              <div class="form-check form-check-light mb-4">
                                  <input class="form-check-input" type="checkbox" id="agree-to-terms" required />
                                  <label class="form-check-label" for="agree-to-terms">
                                      <span class="opacity-70">By joining, I agree to the</span> <a href="#" class="text-light">Terms of use</a> <span class="opacity-70">and</span> <a href="#" class="text-light">Privacy policy</a>
                                  </label>
                              </div>
                              <button class="btn btn-primary btn-lg w-100" type="submit">Sign up</button>
                          </form>

Upvotes: 0

Views: 462

Answers (2)

Lev Coe
Lev Coe

Reputation: 11

I didn't add in core urls.py

from my_app_name import urls
urlpatterns = [
     path('cars/', views.regPage, name="register"),
]

xD

Upvotes: 1

Thierno Amadou Sow
Thierno Amadou Sow

Reputation: 2573

views.py

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from .forms import CreateUserForm
def regPage(request): 
    form = CreateUserForm()
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            # consider adding a redirect page here this is a good approach

    else:
        context = {'form':form}
    return render(request, 'web/car-finder-home.html',context) # your forgot to add context 

Now inside your template you can access the form...

Upvotes: 0

Related Questions