Hi, so i am trying to make an register page by using some custom forms and templates.
now i want my from to be returned to the homepage which it does not for some reason and also want to hide the password in the register page when user types it in, how can i do that :(.
I have done this before using crispy forms but i am not using them at the moment, could it be the custom template is the problem ?
My forms File: from django import formsfrom django.contrib.auth.models import Userfrom - Pastebin.com
My views File: our views here.def register(request): if request.method == 'POST': - Pastebin.com
My Templates File: {% extends 'blog/base.html' %}{% block content %}<div class="container"> - Pastebin.com
My Register Page ( Want to hide the passwords when user types it in): https://i.postimg.cc/pd5zKprv/register.png
Thank you
Couple different things here, and Iâm not sure this list is complete:
-
In your template you have
<input type='{{form.username}}' class="form-control" placeholder="Enter Username">
This is not how you render form fields. See Working with forms - thereâs a lot of information on that page about creating templates and the ways you can render forms in your template.
-
In your form you have:
model = User
fields = '__all__'
Using all in this situation isnât a good idea. Are you aware of all the fields in the User model? Do you want them all rendered and entered in your form? (On top of that, youâre not even properly rendering the form, so the fields youâre identifying as being in the form arenât present.)
-
Iâm guessing youâre not returning to your home page because the form being submitted is failing the is_valid
test. (See the two points above). This would become more âvisibleâ to you if you handled the failing form case with rendering the error messages.
-
The password field is visible because youâre not rendering the password form field correctly. (See #1 above)
If youâre looking for a good example for how to handle those forms, Iâd suggest reading the source for the forms, the views and the templates supplied with Django in django.contrib.auth.
1 Like
For future reference, please post your code / templates / etc here, rather than referencing external sites. You can post your code between lines consisting of three backtick - ` characters. This means youâll have one line of ```, then your code, then another line of ```. This will allow the forum software to keep your code formatted properly.
1 Like
hi sorry for the late response, as my topic was getting approved.
so i have edited the views,forms and template file but it still does not redirect to home page, can you take a look please
My Forms File:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
email = forms.EmailField()
class meta:
model = User
fields = ['email','password1','password2']
My Views File:
from django.shortcuts import render, redirect
from .forms import UserCreateForm
from django.contrib import messages
# Create your views here.
def register(request):
if request.method == 'POST':
form = UserCreateForm(request.POST)
if form.is_valid():
form.save()
email = form.cleaned_data.get('email')
messages.success(request,f"account created for {email}")
return redirect ('home')
else:
form = UserCreateForm()
context = {'form': form,
'title': 'Register Page'}
return render(request, 'users/register.html', context)
My Templates File:
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<h2>Please Register Here </h2>
<form method="post">
{% csrf_token %}
<div class="form-group">
{{ form.email|as_crispy_field }}
</div>
<div class="form-group">
{{ form.password1|as_crispy_field }}
</div>
<div class="form-group">
{{ form.password2|as_crispy_field }}
</div>
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</form>
</div>
{% endblock content %}
EDIT 2: SO i Finally figured it out:
ok finally i figured it out.
class meta, i had to put class Meta (capital M)
and also fields = [âemailâ,âpassword1â,âpassword2â]
i can register users now by email
1 Like