Reputation: 113
The form action is not redirecting from home.html to password.html in Django 2 even I recheck everything including URL pattern Below I am sharing the basic code. My apologies if it's a basic question as I am very new to Django that's why I may not able to detect the issue.
urls.py code
from django.urls import path
from generator import views
urlpatterns = [
path('', views.home),
path('password/', views.password),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request, 'generator/home.html')
def password(request):
return render(request, 'generator/password.html')
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Password Generator</h1>
<form action="password" method="get">
<select name="length">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<input type="button" value="Generate Password">
</form>
</body>
</html>
password.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password</title>
</head>
<body>
<h1>Password page</h1>
</body>
</html>
Upvotes: 0
Views: 589
Reputation: 2018
First of all give names in urls.py so you can access it by name.
urlpatterns = [
path('', views.home,name="index"),
path('password/', views.password,name="password"),
]
in home.html
remove form
's action
in views.py
from django.urls import reverse
def home(request):
if request.method == 'POST':
#you can access input items of form by `request.POST.get('attribute_name')`
# your logic
return redirect(reverse('password')
else:
return render(request, 'generator/home.html')
if still not getting error then please share whole code and what you want to achieve
Upvotes: 1