Issue
views.py:
from django.shortcuts import render
from django.shortcuts import HttpResponseRedirect
from myapp.form import LoginForm
from myapp.models import Login
# Create your views here.
def index(request):
myForm=LoginForm()
if request.method == "POST":
name = request.POST['name']
email=request.POST['email']
password=request.POST['password']
new_data=Login(Name=name,Email=email,Password=password)
new_data.save()
form=LoginForm(request.POST)
if form.is_valid():
return HttpResponseRedirect("thanks")
return render(request,'index.html',{'form':myForm})
def thankyou(request):
return render(request,'thanks.html')
and this is my urls.py:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.index),
path('thanks',views.thankyou),
path('admin/', admin.site.urls),
]
i have created a model template view for sample login form, except home page, everything is fine, even superuser also created.
form.py:
from dataclasses import field
from pyexpat import model
from django import forms
from myapp.models import Login
class LoginForm(forms.Form):
class Data:
model=Login,
field=[
'Name','Email','Password'
]
admin.py:
from django.contrib import admin
# Register your models here.
from myapp.models import Login
admin.site.register(Login)
models.py:
from django.db import models
# Create your models here.
class Login(models.Model):
Name=models.CharField(max_length=20)
password=models.CharField(max_length=20)
base.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
index.html:
{% extends 'base.html' %}
{% block content %}
<div class="box">
<center><h3>Create New Account</h3></center>
<form class="form" action="" method="POST" colspan="4">
{% csrf_token %}
Name : <input type="text" name="name" autocomplete="off" placeholder="name" required>
<br>
<br>
Email : <input type="email" name="email" required placeholder="email" autocomplete="off">
<br>
<br>
Password : <input type="password" name="password" required placeholder="enter new password">
<br>
<input type="submit" name="signup" value="Create">
</form>
<label id="in"><a href="login.html">Already Have Account?</a></label>
<a href="login.html">
<button>Login</button>
</a>
</div>
<div>
</div>
{% endblock content %}
thanks.html:
{% extends 'base.html' %}
{% block content %}
<h1>Thankyou</h1>
{% endblock content %}
so i have creating a login page as i mentioned above, server is running except home page, superuser, admin page also working without issues.migrations done.
Solution
from django.shortcuts import render
from django.shortcuts import HttpResponseRedirect
from myapp.form import LoginForm
from myapp.models import Login
# Create your views here.
def index(request):
myForm=LoginForm()
if request.method=="POST":
name=request.POST['name']
password=request.POST['password']
new_data=Login(Name=name,Password=password)
new_data.save()
form=LoginForm(request.POST)
if form.is_valid():
return HttpResponseRedirect("thanks")
return render(request,'index.html',{'form':myForm}) #your indentation is misplaced
def thankyou(request):
return render(request,'thanks.html')
Answered By - RanBahadurBK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.