2016-11-13 16 views
1

レンダリングされたテンプレートにフォームが表示されません。送信ボタンはそこにありますが、他には何もありません。私は2つのフォームを持って、1つはうまく動作しますEstimateForm私は表示することができません。:Djangoフォーム - 入力ボックスはテンプレートにレンダリングされません

編集:見積もりフォームが基本テンプレートの一部であることを忘れていました。特にウェブサイトのフッターにあります。コンタクトテンプレートがベーステンプレートを拡張しています。基本テンプレートのフォームがテンプレートに表示されません。この情報を最初に載せないと申し訳ありません。

これはこれは

<form role="form" action="" method="post" class="contact-form" style="margin-top: 25px" novalidate="novalidate"> 
    <input type="hidden" name="csrfmiddlewaretoken" value="lfudMn6U8TBJ2czhZM4UZTINnP6xoLZs"> 

    <button type="submit" class="thm-btn">Submit</button> 
</form> 

フォームをレンダリングされるものですテンプレート

<form role="form" action="" method="post" class="contact-form" style="margin-top: 25px"> 
    {% csrf_token %} 
    {{ estimate_form.as_p }} 
    <button type="submit" class="thm-btn">Submit</button> 
</form> 

です。 PY

class ContactForm(forms.Form): 
    contact_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Name*'})) 
    contact_email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Email*'})) 
    contact_phone = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Phone Number*'})) 
    content = forms.CharField(
     required=True, 
     widget=forms.Textarea(attrs={'placeholder': 'Your comments'}) 
    ) 


class EstimateForm(forms.Form): 
    contact_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Name*'})) 
    contact_email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Email*'})) 
    contact_phone = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Your Phone Number*'})) 

    def __init__(self, *args, **kwargs): 
     super(BottomForm, self).__init__(*args, **kwargs) 
     self.fields['contact_name'].label = "" 
     self.fields['contact_email'].label = "" 
     self.fields['contact_phone'].label = "" 

views.py

def contact(request): 
    form_class = ContactForm 

    if request.method == 'POST': 
     form = form_class(data=request.POST) 

     if form.is_valid(): 
      contact_name = request.POST.get(
       'contact_name' 
       , '') 
      contact_email = request.POST.get(
       'contact_email' 
       , '') 
      contact_phone = request.POST.get(
       'contact_phone' 
       , '') 
      form_content = request.POST.get('content', '') 

      # Email the profile with the 
      # contact information 
      template = get_template('contact_template.txt') 
      context = Context({ 
       'contact_name': contact_name, 
       'contact_email': contact_email, 
       'contact_phone': contact_phone, 
       'form_content': form_content, 
      }) 
      content = template.render(context) 
      send_mail('Email from your website', content, context['contact_email'], 
         ['email'], 
         fail_silently=False) 
     return redirect('/contact') 
    return render(request, 'main/contact.html', { 
     'form': form_class, 
    }) 


def estimate(request): 
form_class = EstimateForm 

if request.method == 'POST': 
    form = form_class(data=request.POST) 

    if form.is_valid(): 
     contact_name = request.POST.get(
      'contact_name' 
      , '') 
     contact_email = request.POST.get(
      'contact_email' 
      , '') 
     contact_phone = request.POST.get(
      'contact_phone' 
      , '') 
     form_content = request.POST.get('content', '') 

     # Email the profile with the 
     # contact information 
     template = get_template('contact_template.txt') 
     context = Context({ 
      'contact_name': contact_name, 
      'contact_email': contact_email, 
      'contact_phone': contact_phone, 
      'form_content': form_content, 
     }) 
     content = template.render(context) 
     send_mail('Email from your website', content, context['contact_email'], 
        ['@gmail.com'], 
        fail_silently=False) 
    return redirect('/contact') 
return render(request, 'main/contact.html', { 
    'estimate': form_class, 
}) 
+0

をあなただけの 'ContactForm' form_class''として定義ビュー:すべて一緒にそれを置く

。しかし、あなたは 'estimate_form'を定義していません。だから、あなたはそれを見ません。 –

答えて

1

あなたがキー '見積もり'

return render(request, 'main/contact.html', { 
    'estimate': form_class, 
}) 

でフォームを渡している。しかし、テンプレートにあなたが

{{ estimate_form.as_p }} 
でアクセスしようとしています

に修正してください

0

ビューでは、ContactFormform_classと定義します。しかし、あなたはestimate_formを定義していないので、それが見えません。 ので、あなたのビューは次のように見ている:

def contact(request): 
form_class = ContactForm 

if request.method == 'POST': 
    form = form_class(data=request.POST) 

    if form.is_valid(): 
     contact_name = request.POST.get(
      'contact_name' 
      , '') 
     contact_email = request.POST.get(
      'contact_email' 
      , '') 
     contact_phone = request.POST.get(
      'contact_phone' 
      , '') 
     form_content = request.POST.get('content', '') 

     # Email the profile with the 
     # contact information 
     template = get_template('contact_template.txt') 
     context = Context({ 
      'contact_name': contact_name, 
      'contact_email': contact_email, 
      'contact_phone': contact_phone, 
      'form_content': form_content, 
     }) 
     content = template.render(context) 
     send_mail('Email from your website', content, context['contact_email'], 
        ['email'], 
        fail_silently=False) 
    return redirect('/contact') 
return render(request, 'main/contact.html', { 
    'form': form_class, 'estimate_form': estimate_form 
}) 
0

ないそれらのすべてはあなたがについて尋ねている問題に関連しているが、あなたは、ここでは少なくとも4つの問題を抱えています。

まず、インスタンスではなくフォームクラスをテンプレートに渡すことです。最初にクラスをインスタンス化する必要があります。

第2に、それをestimateという名前でテンプレートに渡しますが、テンプレートではそれをestimate_formと呼んでいます。同じ名前を使用する必要があります。

第3に、フォームが有効でない場合、エラーが発生したインスタンス化されたフォームをテンプレートに戻すことはないので、ユーザーはそれがなぜ有効でないのかを決して知らないでしょう。

第4に、フォームが有効な場合は、form.cleaned_dataのデータをPOSTから直接取得する必要があります。

def estimate(request): 
    form_class = EstimateForm 

    if request.method == 'POST': 
     form = form_class(data=request.POST) 

     if form.is_valid(): 
      contact_name = form.cleaned_data['contact_name'] 
      contact_email = form.cleaned_data['contact_email'] 
      contact_phone = form.cleaned_data['contact_phone'] 
      form_content = form.cleaned_data['content'] 

      # Email the profile with the 
      # contact information 
      template = get_template('contact_template.txt') 
      context = Context({ 
       'contact_name': contact_name, 
       'contact_email': contact_email, 
       'contact_phone': contact_phone, 
       'form_content': form_content, 
      }) 
      content = template.render(context) 
      send_mail('Email from your website', content, context['contact_email'], 
         ['[email protected]'], 
         fail_silently=False) 
     return redirect('/contact') 
    else: 
     form = form_class() 
    return render(request, 'main/contact.html', { 
     'estimate_form': form, 
    }) 
+0

これはうまくいかなかったのです。見積もりフォームが基本テンプレートの一部であることは忘れています。特に、ウェブサイトのフッターにあります。コンタクトテンプレートがベーステンプレートを拡張しています。基本テンプレートのフォームがテンプレートに表示されません。この情報を最初に載せてくれて申し訳ありません。 –

+0

あなたは何を意味するのか分かりません。あなたは完全なテンプレートを投稿して、あなたが描いているものとその方法を正確に説明する必要があります。 –

関連する問題