gpt4 book ai didi

Python 错误 : The view form. formapp.views.contact 未返回 HttpResponse 对象。解决

转载 作者:行者123 更新时间:2023-11-28 18:49:57 26 4
gpt4 key购买 nike

我是 python 的新手,我正在使用 View 、url 来处理表单...我的项目名称是 projec1,我的应用程序名称是 usersapp

这是我的 **usersapp/views.py **

from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from users.form import ContactForm

def contact(request):
if request.method == "POST":
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
success = True
username = contact_form.cleaned_data['username']
password = contact_form.cleaned_data['password']
else:
contact_form = ContactForm()
return render_to_response('contact.html', {'contact_form': contact_form})

这是我的usersapp/contact.html

{%block title%}
Contact
{%endblock%}
{%block content%}
<h2>Contact</h2>
<form action ='.' method = 'POST'>
{{contact_form.as_p}}
<input type ="Submit" name = "submit" value = "send">
</form>
{%endblock%}

这是我的usersapp/form.py

from django import forms

class ContactForm(forms.Form):
username = forms.CharField()
password = forms.CharField()

这是我的projec1/urls.py

 from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import *
from users.views import login
from users.views import contact

urlpatterns = patterns('',
('^contact/$', contact),
)

我正面临这个错误

  TypeError at /
Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.4.3
Exception Type: TypeError
Exception Value:
Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)

我的错误是:

  NameError at /contact
name 'forms' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000/contact
Django Version: 1.4.3
Exception Type: NameError
Exception Value:
name 'forms' is not defined
Exception Location: C:\Users\Documents\MyProjects\projec1\users\form.py
in<module>,line 2
Python Executable: C:\Python27\python.exe

请指导某人,提前谢谢

最佳答案

表单类定义错误,可能导致该错误:

from django import forms

# Inherit from `forms.Form`, not from `forms`:
class ContactForm(forms.Form):
username = forms.CharField()
password = forms.CharField()

在 View 函数中还有一些问题:

def contact(request):
if request.method == "POST":
contact_form = ContactForm(request.POST)

# Not `contact_form is valid()`:
if contact_form.is_valid():
success = True

# `cleaned_data` is all lowercase:
username = contact_form.cleaned_data['username']
password = contact_form.cleaned_data['password']
else:
contact_form = ContactForm()

# You always want to return a response. Putting the return statement
# here will fix the error mentioned in the question's title. The
# second argument makes `contact_form` available from the template:
return render_to_response('contact.html', {
'contact_form': contact_form
})

关于Python 错误 : The view form. formapp.views.contact 未返回 HttpResponse 对象。解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497158/

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