python - Django 表单不从 HomeForm 类呈现 HTML
问题描述
出于某种原因,我无法让我的 Django 表单呈现。我不确定我错过了什么。
我正在尝试class HomeForm(forms.Form)
在页面上呈现,但myaccount/change.html
没有成功。
我是 Django 新手,所以我可能在某处遗漏了一个小细节,但我认为我从 djago 文档中得到了所有内容。
如果有人可以帮助我,将不胜感激。谢谢!
用户/forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django import forms
from .models import CustomUser
class HomeForm(forms.Form):
post = forms.CharField()
your_name = forms.CharField(label='Your name', max_length=100)
class CustomUserCreationForm(UserCreationForm):
# job_title = forms.IntegerField(label='2 + 2', label_suffix=' =')
# yes = forms.IntegerField(required=True)
tos_check = forms.BooleanField(required=True, label='I have read and agree to the Terms of Service.')
age_check = forms.BooleanField(required=True, label='I am 21 years of age or older.')
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('username', 'email')
help_texts = {
'username': '',
'email': '',
# 'password1': 'None',
# 'password2': 'Test'
}
class CustomUserChangeForm(UserChangeForm):
tos_checktwo = forms.BooleanField(required=True, label='I have read and agree to the Terms of Service.')
class Meta(UserChangeForm.Meta):
model = CustomUser
fields = ('username', 'email', 'tos_checktwo')
# class Meta:
# model = CustomUser
# fields = ('username', 'email', 'tos_check',)
用户/views.py
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.shortcuts import render
from django.template import loader
from django.urls import reverse_lazy
from django.views import generic
from django import forms
from .forms import CustomUserCreationForm
from .forms import HomeForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = HomeForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/home/')
# if a GET (or any other method) we'll create a blank form
else:
form = HomeForm()
return render(request, 'myaccount/change.html', {'form': form})
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
页面/urls.py
from django.urls import path
from django.urls import path, include
from django.conf.urls import url
from django.conf import settings
from . import views
from .views import HomePageView, MyAccountView, AboutPageView, PrivacyPageView, ContactPageView
urlpatterns = [
path('about/', AboutPageView.as_view(), name='about'),
path('privacy/', PrivacyPageView.as_view(), name='privacy'),
path('contact/', ContactPageView.as_view(), name='contact'),
path('', HomePageView.as_view(), name='home'),
path('myaccount/', MyAccountView.as_view(), name='myaccount'),
url('avatar/', include('avatar.urls')),
url('myaccount/', include('avatar.urls')),
]
模板/myaccount/change.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock content %}
解决方案
先生,我在共享的代码中没有发现任何问题。但这里有两个提示:
- 检查 base.html 中是否包含块内容。
- 在视图中的 return 之前添加
print(form)
以查看您得到的内容(打印结果将在终端上而不是浏览器上)
编辑: 您应该添加一个调用视图的 url:在 url 添加此行:
path('testform/', view.get_name, name ='get_name')
然后在你的浏览器上,测试 url127.0.0.1:8080/testform/
推荐阅读
- validation - 聊天机器人:如何验证来自谷歌环聊的传入请求
- pandas - 将多列上的数据框连接到一列公差?
- python - 在 INPUT 语句中包含变量
- c++ - 在给定处理器缓存的情况下,Windows 中的页面写入跟踪有多可靠
- android - Android:添加 Firebase 要求时出现 build.gradle 错误
- javascript - JavaScript Axios GET 数据= null
- oracle - 无法修改映射到 pl/sql 中的非键保留表的列
- c++ - 使用推力使用 sort_by_key 对两个 OpenGL 缓冲区进行排序?
- powerbi - 如何找到一个表中存在而另一表中缺失的值的总和
- c# - 在 UpdatePanel 中搜索控件时出现 C# 错误