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 >
<center><h3>Create New Account</h3></center>
<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.
CodePudding user response:
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')
CodePudding user response:
Your view.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']
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})
def thankyou(request):
return render(request,'thanks.html')
My change
In your index function
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})
For Example
My Django app name is blog
return render(request,'blog/index.html',{'form':myForm})
In your thankyou function
return render(request,'your app name/thanks.html')
Changes in setiing.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
My CRUD project's views.py
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username Or Password is inccorect')
I hope you can understand my answer