Не удалось отрисовать шаблон домашней страницы в проекте DJango mini,
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']
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')
и вот мой 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),
]
я создал модель шаблона представления для образца формы входа, кроме главной страницы, все в порядке, даже суперпользователь также создан.
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','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 %}
<h1>Application Form</h1>
<form action="" method="post">
Name: <input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit">
</fomr>
{% endblock content %}
thanks.html:
{% extends 'base.html' %}
{% block content %}
<h1>Thankyou</h1>
{% endblock content %}
Итак, я создал страницу входа, как я упоминал выше, сервер работает, кроме домашней страницы, страницы суперпользователя, страницы администратора также работают без проблем.Миграции сделаны.
Ваш 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')
Моя перемена
В вашей индексной функции
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})
Пример
Название моего приложения Django - blog
return render(request,'blog/index.html',{'form':myForm})
В вашей функции благодарности
return render(request,'your app name/thanks.html')
Изменения в 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',
],
},
},
]
views.py моего CRUD проекта
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')
Надеюсь, вы сможете понять мой ответ
.
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')