2014-03-19 57 views
1

所以說,我有如下一個Django views.py文件:Django的形式:集成一個提交按鈕,表單的一部分

def addauthorView(request): 
if request.method == 'POST': 
    f = ContactForm(request.POST) 
    if form.is_valid(): 
     first_name = form.cleaned_data['firstname'] 
     last_name = form.cleaned_data['lastname'] 
     user_email = form.cleaned_data['email'] 
     c = Contact(firstname=first_name, lastname=last_name, email=user_email) 
    else: 
     form = ContactForm(request.POST) 
     return render(request, 'addauthor.html', {'form': ContactForm}) 
else: 
    return render(request, 'addauthor.html', {'form': ContactForm}) 

一個forms.py文件中像這樣

class ContactForm(forms.Form): 
    firstname = forms.CharField(max_length=50) 
    lastname =forms.CharField(max_length=50) 
    email = forms.EmailField() 

和HTML文件中像這樣

<html> 
<head> 
    <title>Head</title> 
</head> 

<body> 
    <ul> 
    {{form.as_ul}} 
    <input type="submit"> 
    </ul> 
</body> 

如何使按下<input type="submit">按鈕時,我的ContactForm視圖將執行一些代碼。有沒有一種將按鈕分組到form.py的特定方法?或者還有其他方法可以做到這一點。如果有人可以幫我修改這段代碼,那就太好了。

在此先感謝。

回答

1

只需附上form.as_ulsubmit按鈕進入form標籤:

<form action="/your_url_here/" method="post">{% csrf_token %} 
{{ form.as_ul }} 
<input type="submit" value="Submit" /> 
</form> 

另見Displaying a form using a template

希望有所幫助。

+0

謝謝,我幾乎認爲這不是那麼容易; P – ApathyBear