The below codes are the codes i used to create my register form. When i try to go to 127.0.0.1:8000/register/
, this was redirecting to my home page i mean it render my index.html page i cant find find any mistakes in my code. And, i dont know why this is redirecting to my home page. Even i try to new virtual env, copy my files to my friend laptop, reinstalled my browser but nothing works. Its been a weak still i can’t find what is happening here.
urls.py
path('register/', views.register, name ='register')
views.py
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
email = form.cleaned_data.get('email')
htmly = get_template('registration/email.html')
d = { 'username': username }
subject, from_email, to = 'Welcome', 'myemail@gmail.com', email
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
messages.success(request, f'Your account has been created ! You are now able to log in')
return redirect('login')
else:
form = UserRegisterForm(request.POST)
return render(request, 'registration/register.html', {'form':form})
form.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = {'username', 'email', 'password1', 'password2'}
def save(self, commit=True):
user = super(UserRegisterForm, self).save(commit=False)
user.name = self.cleaned_data['name']
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
register.html
{% extends "index.html" %}
{% load crispy_forms_tags %}
{% block start %}
<div class="container">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form }}
</table>
<button type="submit">Submit</button>
</form>
<sub><a href="{% url 'login' %}">Login to your Account</a></sub>
</div>
{% endblock start %}
This was the response I get when I try to go to the page.
[29/Jul/2021 07:43:33] "GET /register/ HTTP/1.1" 200 1317