我正在使用django和postgresql为uni项目开发Web应用程序。这是我第一次使用django表单,当尝试渲染其中任何一个进行测试运行并找出视图时,都会出现上述错误消息。
我之前已经使用Django,但这是我第一次使用表单-以前,我已经完成了对象创建和通过API视图的编辑。我找不到我的代码错误的地方,但是我尝试重新编写代码,四处移动以查看它是否有帮助(没有帮助),甚至避免继承,并尝试将其写成简单的形式(没有)我有四种不同的形式,但是它们都提供相同的错误消息,所以我只想展示一个用户模型扩展,举例来说,如果我错得比我多,我对此不太有把握。我已经意识到。
这是我得到的代码
class JdTform(UserCreationForm):
telefono = forms.IntegerField()
class Meta(UserCreationForm.Meta):
fields = [
'telefono'
]
labels = {
'telefono': 'teléfono'
}
widgets = {
'telefono': forms.NumberInput()
}
def jdt_form_view(request):
if request.method == 'GET':
form = JdTform()
return render(request, 'personas/formu_jdt.html', {'form': form})
if request.method == 'POST':
form = JdTform(request.POST)
if form.is_valid():
user = form.save()
fono = form.cleaned_data.get('telefono')
jdt = jefeDeTaller(telefono=fono, u=user)
jdt.save()
return redirect('cal_fecha')
<html lang="en">
<head>
<meta charset="utf-8">
<title>Gestor de Trabajo</title>
</head>
<body>
<header>
<h1>My Site</h1>
</header>
<main>
<h2>Sign up</h2>
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</main>
</body>
</html>
urlpatterns = [
path('calendario/<int:ano>/<int:mes>/', views.calendario_mes, name='cal_fecha'),
path('usuarios/jefedetaller/', views.jdt_form_view, name='formulario_jdt'),
我在调试页面上收到的错误消息是
AttributeError at /usuarios/jefedetaller/
'render' object has no attribute 'get'
带有追溯
Request Method: GET
Request URL: http://127.0.0.1:8000/usuarios/jefedetaller/
Django Version: 2.2.1
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gestor']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Flavio\Envs\3dmente\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Flavio\Envs\3dmente\lib\site-packages\django\utils\deprecation.py" in __call__
96. response = self.process_response(request, response)
File "C:\Users\Flavio\Envs\3dmente\lib\site-packages\django\middleware\clickjacking.py" in process_response
26. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /usuarios/jefedetaller/
Exception Value: 'render' object has no attribute 'get'
使用此特定模型,其想法是创建一个用户对象,将其直接与jdt对象链接,并通过此实例传递fono信息。
非常感谢您的帮助!