2017-08-24 21 views
0

レンダリングされていないフォームがあり、その理由がわかりません。表示されるのは送信ボタンだけです。私は、herehereおよびhereと記載された方法論に従ったフォームを作成しました。DjangoフォームがHTMLでレンダリングされない

私はこの問題の解決策を見ていましたが(他にも下に掲載されています)、彼らは助けにならなかった。

django-forms not rendering errors

django form not rendering in template. Input fields doesn't shows up

Django Form not rendering

Django Form not rendering - following documentation

HTMLは、HTML another- landing_page/base.html

を延びるapp_core/index.htmlにある:

{% extends 'landing_page/base.html' %} 
{% load i18n %} 
{% load staticfiles %} 
{% load static %} 
{% load bootstrap %} 

{%block content %} 
<div id="contactus" class="container-fluid"> 

<br> 

<div class="container text-center"> 

    <div class="row"> 
     <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2 text-left"> 
      <center><h3>{% trans 'Contact Us' %}</h3> 
      <p>{% trans 'We are at your disposal 365 days 24 hours a day. When you think of languages think of Milingual. 
          Languages are not studied, they are lived!' %}</p></center> 
     </div> 
    </div> 
    <div class ="row"> 
     <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2 text-left"> 
     <center><h1>Contact Us</h1><center> 

     <form id="contactus-form" action="{% url 'contact' %}"method="post" enctype="multipart/form-data"> 
      {% csrf_token %} 
      {{ form.as_p }} 

      <br/> 
      <div class="form-actions"> 
       <button type="submit" class="btn btn-primary pull-center">Send</button> 
      </div> 
     </form> 
     <div> 

    </div> 

</div> 


{%endblock content %} 

Views.py

from django.core.mail import BadHeaderError 
from django.http import HttpResponse, HttpResponseRedirect 
from django.shortcuts import render, redirect 
from .forms import ContactForm 
#ContactUs 
def contact(request): 
    if request.method == 'GET': 
    form = ContactForm() 
else: 
    form = ContactForm(request.POST) 
    if form.is_valid(): 
     whoareyou = form.cleaned_data['whoareyou'] 
     name = form.cleaned_data['name'] 
     phone_number = form.cleaned_data['phone_number'] 
     subject = form.cleaned_data['subject'] 
     from_email = form.cleaned_data['from_email'] 
     message = form.cleaned_data['message'] 
     try: 
      send_email(subject, message, whoareyou, from_email, ['[email protected]']) 
     except BadHeaderError: 
      return HttpResponse('Invalid header found.') 
     return redirect('success') 
return render(request, "/index.html", {'form': form}) 


def success(request): 
    return HttpResponse('Success! Thank you for your message.') 

form.py

from django import forms 


class ContactForm(forms.Form): 
    WHOAREYOU_CHOICES = (
     ('Teacher', 'Teacher'), 
     ('Student', 'Student'), 
     ('Venue', 'Venue'), 
     ('Business', 'Business'), 
     ('Other', 'Other') 
    ) 
    whoareyou = forms.ChoiceField(choices=WHOAREYOU_CHOICES, required=True) 
    name = forms.CharField(required=True) 
    phone_number = forms.CharField(required=True) 
    from_email = forms.EmailField(required=True) 
    subject = forms.CharField(required=True) 
    message = forms.CharField(widget=forms.Textarea, required=True) 

そしてurls.py

from django.conf.urls import url, include 
from .views import * 
from app_core import views 

urlpatterns = [ 
    url(r'^$', IndexPage, name='index'), 
    # setting session city 
    url(r'^get-city-session$', GetCitySession, name='get-city-session'), 
    url(r'^set-city-session$', SetCitySession, name='set-city-session'), 
    url(r'^contact/$', views.contact, name='contact'), 
    url(r'^success/$', views.success, name='success'), 
] 

答えて

2

ブロック内にコードを配置する必要があります。そうでない場合は、コードを配置する場所がわかりません。あなたのbase.htmlでは、

{% block body %} 
{% endblock %} 

ような何かを行うことができます。そして、あなたのindex.htmlページに、あなたはあなたのベースにあるその場所に表示したいすべてのものを囲む必要があります。

{% block body %} 
    ... Code goes here ... 
{% endblock %} 
+0

inはすでにブロックされています。私はここにそれを追加することを怠っていた。残りのHTMLは完全にレンダリングされます。ちょうどフォームではない – fmakawa

関連する問題