2013-01-24 113 views
3

我在python新手,我用的意見,網址形式的工作... 我的項目名稱是projec1和我的應用程序名稱是usersappPython錯誤:視圖form.formapp.views.contact沒有返回HttpResponse對象。解決

這是我的** 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), 
        ) 

IM面臨這樣的錯誤

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 

PLZ指導別人,謝謝你提前

+0

你應該包括完整的堆棧跟蹤。沒有它,發生異常的地方並不明顯。 –

回答

1

窗體類定義是錯誤的,這可能導致該錯誤:

from django import forms 

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

也有一對夫婦在視圖功能問題:

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 
    }) 
+0

即時得到錯誤,我做了修改,並再次運行服務器,以查看我的預期輸出。錯誤是:無效的語法(form.py,第2行),它向我展示了異常區域「users \ views.py ,第4行」 – raghu

+0

您可以發佈錯誤,您得到... – Prateek

+0

SyntaxError at/ 無效的語法(form.py,2號線) 請求方法:\t GET 請求URL:\t http://127.0.0.1:8000/ Django的版本:\t 1.4.3 異常類型:\t的SyntaxError 異常值:\t 語法無效(form.py,第2行) 異常位置:\t C:\ Users \ Documents \ MyProjects \ projec1 \ users \ views.py ,第4行 – raghu

相關問題