gpt4 book ai didi

python - html 文件中的 {{ form }} 未渲染

转载 作者:行者123 更新时间:2023-12-01 09:26:18 24 4
gpt4 key购买 nike

url.py

#python -m smtpd -n -c DebuggingServer localhost:1025
from django.contrib import admin
from .import views
from django.conf.urls import url,include
from django.contrib.auth.views import (
login,logout,
password_reset,
password_reset_done,
password_reset_confirm,
password_reset_complete
)
#from django.urls import path

urlpatterns = [
url(r'^$',views.home),
url(r'^login/$',login,{'template_name' : 'accounts/login.html'},name='login'),
url(r'^logout/$',logout,{'template_name' : 'accounts/logout.html'}),
url(r'^register/$',views.register,name='register'),
url(r'^profile/$',views.view_profile,name='view_profile'),
url(r'^profile/edit/$',views.edit_profile,name='edit_profile'),
url(r'^change-password/$',views.change_password,name='change_password'),

url(r'^reset-password/$',password_reset,{'template_name':'accounts/reset_password.html',
'post_reset_redirect':'accounts:password_reset_done','email_template_name':'accounts/reset_password_email.html'},name='reset_password'),

url(r'^reset-password/done/$',password_reset_done,{'template_name':'accounts/reset_password_done.html'},name='password_reset_done'),

url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
password_reset_confirm,{'template_name':'accounts/reset_password_confirm.html','post_reset_redirect':'accounts:password_reset_complete'},name='password_reset_confirm' ),

url(r'^reset-password-complete/$',password_reset_complete,{'template_name':'accounts/reset_password_complete.html'},name='password_reset_complete'),



]

urls.py(教程)

from django.contrib import admin
from django.conf.urls import url,include
from tutorial import views
from django.conf import settings
from django.conf.urls.static import static
#from django.urls import path

urlpatterns = [
url('admin/', admin.site.urls),
url('accounts/',include('accounts.urls',namespace='accounts')),

url(r'^$',views.login_redirect,name='login_redirect'),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

View .py

from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm
from tutorial import views
from accounts.form import Registrationform,EditProfileForm
from django.contrib.auth.forms import UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from .forms import HomeForm


class HomeView(TemplateView):
def get(self,request):
form = HomeForm(request.get)
return render(request, 'accounts/home.html', {'form': form})


@login_required
def home(request):
numbers=[1,2,3,4,5]
name="jay"
args={'myName':name}
return render(request,'accounts/home.html',args)

def register(request):
if request.method == "POST":
form = Registrationform(request.POST)
if form.is_valid():
form.save()
return redirect('/accounts')
else:
return HttpResponse('please fill all the fields and make sure new password must be match')
else:
form = Registrationform()
args={'form' : form}
return render(request,'accounts/reg_form.html',args)
@login_required
def view_profile(request):
args = {'user' : request.user}
return render(request,'accounts/profile.html',args)
@login_required
def edit_profile(request):
if request.user.is_authenticated():
#<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
if request.method=="POST":
form = EditProfileForm(request.POST,instance=request.user)
if form.is_valid():
form.save()
return redirect('/accounts/profile')
else:
return HttpResponse("please go back and write correct vaues")
else:
form = EditProfileForm(instance=request.user)
args = {'form' : form}
return render(request,'accounts/edit_profile.html',args)
else:
HttpResponse("hllo")
return redirect('/accounts/login')
@login_required
def change_password(request):
if request.user.is_authenticated():
if request.method == "POST":
form = PasswordChangeForm(data=request.POST,user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect('/accounts/profile')
else:
return HttpResponse("password does not match,go back and try again")
else:
form = PasswordChangeForm(user=request.user)
args = {'form' : form}
return render(request,'accounts/change_password.html',args)
else:
return redirect ('/accounts/login')

表单.py

from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm
from tutorial import views
from accounts.form import Registrationform,EditProfileForm
from django.contrib.auth.forms import UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from .forms import HomeForm


class HomeView(TemplateView):
def get(self,request):
form = HomeForm(request.get)
return render(request, 'accounts/home.html', {'form': form})


@login_required
def home(request):
numbers=[1,2,3,4,5]
name="jay"
args={'myName':name}
return render(request,'accounts/home.html',args)

def register(request):
if request.method == "POST":
form = Registrationform(request.POST)
if form.is_valid():
form.save()
return redirect('/accounts')
else:
return HttpResponse('please fill all the fields and make sure new password must be match')
else:
form = Registrationform()
args={'form' : form}
return render(request,'accounts/reg_form.html',args)
@login_required
def view_profile(request):
args = {'user' : request.user}
return render(request,'accounts/profile.html',args)
@login_required
def edit_profile(request):
if request.user.is_authenticated():
#<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
if request.method=="POST":
form = EditProfileForm(request.POST,instance=request.user)
if form.is_valid():
form.save()
return redirect('/accounts/profile')
else:
return HttpResponse("please go back and write correct vaues")
else:
form = EditProfileForm(instance=request.user)
args = {'form' : form}
return render(request,'accounts/edit_profile.html',args)
else:
HttpResponse("hllo")
return redirect('/accounts/login')
@login_required
def change_password(request):
if request.user.is_authenticated():
if request.method == "POST":
form = PasswordChangeForm(data=request.POST,user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect('/accounts/profile')
else:
return HttpResponse("password does not match,go back and try again")
else:
form = PasswordChangeForm(user=request.user)
args = {'form' : form}
return render(request,'accounts/change_password.html',args)
else:
return redirect ('/accounts/login')

home.html

{% extends 'base.html' %}


<!DOCTYPE html>

<head>
{% block head %}
<title>home</title>
{% endblock %}
</head>

{% block body %}
<div class="container">

<form method="post">
{% csrf_token %}
{{ form.as_p }}

<button type="submit" >Submit</button>
</form>
</div>

{% endblock %}
</html>

和浏览器网址

enter image description here

我的问题是 home.html 文件中的 {{ form }} 没有被渲染。{{ form.as_p }} 也不起作用。

表单标签(输入)无法出现在网页(浏览器)中。

终端中没有显示任何错误。

请帮我找出错误。

最佳答案

其工作原理是将类更改为函数。

def HomeView(request):
form = HomeForm()
return render(request, 'accounts/home.html', {'form': form})

也改变了

url(r'^$',views.HomeView)

在 urls.py 中

关于python - html 文件中的 {{ form }} 未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com