2017-01-27 7 views
0

私はDjango forms 'tutorial'を使っています。チュートリアルを読んだ後は、私のニーズに合わせて修正して、ジャンゴがうまく作成できるようにカスタマイズしました。私はフォームを修正するたびにウェブサイトが更新されないことを発見しました。私は自分のコードに誤りがあると思っていますが、私はそれを見つけることができませんでした。Django form.pyが更新されない

# views.py 
def contact(request): 
    # if this is a POST request we need to process the form data 
    if request.method == 'POST': 
     # create a form instance and populate it with data from the request: 
     form = ContactForm(request.POST) 
     # check whether it's valid: 
     if form.is_valid(): 
      # process the data in form.cleaned_data as required 
      # ... 
      # redirect to a new URL: 
      return HttpResponseRedirect('/message_recived/') 

# forms.py 
from django import forms 
class ContactForm(forms.Form): 
    name = forms.CharField(label='Name', max_length=100) 
    email = forms.EmailField(label='Email', max_length=100) 
    message = forms.CharField(label='Message', max_length=500) 


# models.py 
from django.db import models 
class Contact(models.Model): 
    name = models.CharField(max_length=100) 
    email = models.CharField(max_length=100) 
    message = models.CharField(max_length=500) 

、ここcontact.htmlテンプレートです:

#contact.html 
{% extends "BlogHome/headerAndFooter.html" %} 
{% block content %} 
<script> 
document.title = "Pike Dzurny - Contact" 
</script> 
<form action="/message_recived/" method="post"> 
    {% csrf_token %} 
    {{ form }} 
    <input type="submit" value="Submit" /> 
</form> 
{% endblock %} 

は、私が何か間違ったことをしましたか?私は、新しいブラウザを使用してブラウザのキャッシュをクリアし、明らかにそれをリフレッシュしようとしました。

答えて

1

renderあなたの眺めの中であなたの応答を忘れているように見えます。 テンプレートを正しく表示するには、コンテキストにフォームを含める必要があります。

は、次のように表示を変更してください:

def contact(request): 
    # if this is a POST request we need to process the form data 
    if request.method == 'POST': 
     # create a form instance and populate it with data from the request: 
     form = ContactForm(request.POST) 
     # check whether it's valid: 
     if form.is_valid(): 
      # process the data in form.cleaned_data as required 
      # ... 
      # redirect to a new URL: 
      return HttpResponseRedirect('/message_recived/') 
    else: 
     form = ContactForm()  
    return render(request, 'contact.html', {'form': form}) 
+0

まだ何も): –

+1

は@PikeD。 hm、テンプレートが正しいかどうかをブラウザで確認することができますか?ページにフォームが表示されますか? – neverwalkaloner

+0

私はちょうど私のテンプレートを見ました。テンプレートの複製があり、「contact.html」と他の「Contact.html」を除いて、私は 'Contact.html'を編集していたようです。私はHTMLをコピーして 'contact.html'に貼り付けました。そして、まだフォームをレンダリングしていないようです。 –

関連する問題