Django表示不使用include标签显示的字段

时间:2018-02-25 14:37:00

标签: django forms django-forms contact-form

我花了几天时间试图找到问题的答案。我一直在阅读并尝试给有类似问题的用户提供答案,但没有一个能够奏效。

我创建了一个联系表单,如果我打开表单代码所在的html,它就能完美运行。但是当我尝试使用include标签在索引上显示它时,它会显示提交按钮,结构和样式,但不显示表单字段。

这就是我在代码中的内容:

views.py

    from django.http import HttpResponse
    from django.views import generic
    from django.views.generic.edit import CreateView, UpdateView, DeleteView
    from django.core.mail import send_mail
    from .forms import ContactForm
    from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'landingpage/index.html', {})

#Contact form
def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = request.POST.get ('name')
            email = request.POST.get ('email')
            message = request.POST.get ('message')

            send_mail('Subject here', content, contact_email, [‘xxx@gmail.com'], fail_silently=False)
            return HttpResponseRedirect('/thanks/')
    else:
        form = ContactForm()
    return render(request, 'landingpage/contact.html', {'form': form})

#Thank you message
def thanks (request):
    return render(request, 'landingpage/thanks.html', {})

urls.py

app_name = 'landingpage'
urlpatterns = [
    # Landingpage urls
    url(r'^$', views.index, name='landing-index'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^thanks/$', views.thanks, name='thanks'),
]

的index.html

{% block form %}

        {% include 'landingpage/contact.html' with form=form %}

{% endblock form %}

contact.html

{% block form %}
<section id="contact">
      <div class="container">
        <div class="row">
          <div class="col-lg-12 text-center">
            <h2 class="section-heading text-uppercase">Contact Us</h2>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-12">
            {% if form.errors %}
                <p style="color: red;">
                    Please correct the error{{ form.errors|pluralize }} below.
                </p>
               {% endif %}
            <form id="contactForm" name="sentMessage" action="" method="post" novalidate>
              {% csrf_token %}

                {% for hidden_field in form.hidden_fields %}
                  {{ hidden_field }}
                {% endfor %}

                {% if form.non_field_errors %}
                  <div class="alert alert-danger" role="alert">
                    {% for error in form.non_field_errors %}
                      {{ error }}
                    {% endfor %}
                  </div>
                {% endif %}

                {% for field in form.visible_fields %}
                  <div class="form-group">
                    {{ field.label_tag }}

                    {% if form.is_bound %}
                      {% if field.errors %}
                        {% render_field field class="form-control is-invalid" %}
                        {% for error in field.errors %}
                          <div class="invalid-feedback">
                            {{ error }}
                          </div>
                        {% endfor %}
                      {% else %}
                        {% render_field field class="form-control is-valid" %}
                      {% endif %}
                    {% else %}
                      {% render_field field class="form-control" %}
                    {% endif %}

                    {% if field.help_text %}
                      <small class="form-text text-muted">{{ field.help_text }}</small>
                    {% endif %}
                  </div>
                {% endfor %}

                <div class="clearfix"></div>
                <div class="col-lg-12 text-center">
                  <div id="success"></div>
                  <button id="sendMessageButton" class="btn btn-primary btn-xl text-uppercase" type="submit">Send Message</button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
</section>
{% endblock form%}

1 个答案:

答案 0 :(得分:1)

这是因为您的索引视图在其上下文中没有表单变量。你应该这样写:

def index(request):
     ctx = {
       'form':  ContactForm()
     }
     return render(request, 'landingpage/index.html', ctx)