gpt4 book ai didi

python - Django - 'ContactForm' 对象没有属性 'get'

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:35 25 4
gpt4 key购买 nike

无论我以何种方式实现我的表单,我都会收到以下错误。当我转到表单的 URL 时会发生这种情况。

表单.py

class ContactForm(forms.ModelForm):

class Meta:
model = ContactUs
fields = ('name', 'email', 'phone', 'message')

模型.py

class ContactUs(models.Model):
name = models.CharField(max_length=50, blank=True)
email = models.EmailField()
phone = models.CharField(max_length=15, blank=True)
message = models.TextField()
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)

class Meta:
verbose_name_plural = "Contact Us"

View .py

def contact(request):
if request.method == "POST":
form = ContactForm(request.POST or None)
errors = None
if form.is_valid():
ContactUs.objects.create(
name = form.cleaned_data.get('name'),
email = form.cleaned_data.get('email'),
phone = form.cleaned_data.get('phone'),
message = form.cleaned_data.get('message'),
created_at = form.cleaned_data.get('created_at')
)
return HttpResponseRedirect("/s/")
if form.errors:
errors = form.errors

template_name = 'contact_us.html'
context = {"form": form, "errors": errors}
return render(request, template_name, context)

网址.py

url(r'^contacts/$', views.ContactForm, name='contact_form'),

html

<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-warning">Submit</button>
</form>

提前致谢!

最佳答案

您已将 URL 指向表单,而不是 View 。应该是:

url(r'^contacts/$', views.contact, name='contact_form'),

请注意,一旦您修复了此问题,您将遇到另一个问题,因为您的 View 不会为 GET 请求返回任何内容。

关于python - Django - 'ContactForm' 对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45563608/

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