Issue
I would like to view the registration form directly on the index.html
page (home page). In index.html, i would like to display register.html
.
The registration form was created in registration/register.html. I call it in index.html using:
{% block content %}
{% include 'registration/register.html' %}
{%endblock%}
In the urlpatterns
of App1/views.py
, i use:
path("register_request", views.register_request, name='register_request')
The problem is that on the index.html i see the form button, but not its textboxes (username, email, password).
I can't understand where I'm wrong. Can you tell me what i'm doing wrong and can you show me code how to fix it?
This is App1
:
App1
.migrations
.static
.templates
..index.html
..registration
...register.html
index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>University</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
<link href="/static/css/style.css" rel="stylesheet">
</head>
<body>
.......
.......
{% block content %}
{% include 'registration/register.html' %}
{%endblock%}
</body>
</html>
App1/templates/registration/register.html
<!--Register-->
<div class="container py-5">
<h1>Register</h1>
<form method="POST">
{% csrf_token %}
{{ register_form.as_p }}
<button class="btn btn-primary" type="submit">Register</button>
</form>
<p class="text-center">If you already have an account, <a href="/login">login</a> instead.</p>
</div>
App1/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
# Create your forms here.
class NewUserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
App1/urls.py
from django.urls import path
from . import views
urlpatterns=[
path('', views.index, name='index'),
path("register_request", views.register_request, name='register_request'),
]
App1/views.py
from .forms import NewUserForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
def index(request):
"""View function for home page of site."""
return render(request, 'index.html')
def register_request(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful." )
return redirect("index.html")
messages.error(request, "Unsuccessful registration. Invalid information.")
form = NewUserForm()
return render (request=request, template_name="registration/register.html", context={"register_form":form})
#NO: return render(request, "registration/register.html",{"register_form":form})
MYSITE (PROJECT)
mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.contrib.auth.views import LoginView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App1.urls')),
mysite/setting.py
TEMPLATES = [
{
....
#NO: "DIRS" : [ BASE_DIR / "templates" ],
'DIRS': ["templates"],
[
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
In models.py
i didn't write anything.
Solution
The issue is with how you're rendering the registration form. Since you want the form on the index page, there's no need for a separate view (register_request
). Remove that view and handle the form processing directly in your index
view. I've made some adjustments in the code snippet below.
# In App1/views.py
from .forms import NewUserForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
def index(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful.")
return redirect("index")
else:
messages.error(request, "Unsuccessful registration. Invalid information.")
else:
form = NewUserForm()
return render(request, 'index.html', {'register_form': form})
Remove the register_request
path in App1/urls.py
:
# In App1/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
These changes should display the registration form properly on your index page.
Answered By - godd0t
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.