[英]Django error - 'function' object has no attribute 'MyForm'
I am new to Django and was working with Django forms and stuck at an error for which I am not finding any relevant solution.我是 Django 的新手,正在使用 Django 表单并遇到一个错误,我没有找到任何相关的解决方案。
My Form.py is :我的 Form.py 是:
from django import forms
class MyForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)
and My views.py is :我的 views.py 是:
from django.shortcuts import render
from django.http import HttpResponse
from . import forms
# Create your views here.
def index(request):
return render(request, 'basicapp/index.html')
def forms(request):
form = forms.MyForm()
return render(request, 'basicapp/forms.html', context = { 'form' : form
})
All the routing is Fine as I have checked by replacing forms with a normal HttpResponse but there is some problem in the forms.py or views.py as they the form in not being displayed in the browser and the Error is coming所有路由都很好,因为我已经通过用普通的 HttpResponse 替换表单进行了检查,但是在 forms.py 或 views.py 中存在一些问题,因为它们没有在浏览器中显示表单并且错误即将到来
'function' object has no attribute 'MyForm'
Please Someone help :( I gotta move Forward请有人帮忙:(我得继续前进
Change the name of the view def forms(request)
to something else like def formView()
Your function is calling itself when you do forms.Myform()
.将视图
def forms(request)
的名称更改为def formView()
类的名称,当您执行forms.Myform()
时,您的函数正在调用自身。 Hence the 'function' object has no attribute 'MyForm'
因此
the 'function' object has no attribute 'MyForm'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.