Ben2pop
Ben2pop

Reputation: 756

Django form not rendering properly

I am following the documentation of the Django Forms but I do not know why my form does not want to show up ! I am creating a form that will get an email en create invitation for user to sign in using this app :https://github.com/bee-keeper/django-invitations

My forms.py:

class InviteForm(forms.Form):
     email1 = forms.EmailField(label='Email 1')

My Views.py:

from django.shortcuts import render
from django.views.generic import TemplateView
from .forms import InviteForm


class candidateIndex(TemplateView):
    template_name= 'candidateIndex.html'

class HRIndex(TemplateView):
    template_name= 'HRindex.html'

def create_invite(request):
    if request.method == 'POST':
        form = InviteForm(request.POST)
        if form.is_valid:
            email = form.cleaned_data['email1']
            invite = Invitation.create('form.email1')
            invite.send_invitation(request)
            print("The mail was went")
        else:
            print("Your form is not valid")
    else:
       form = InviteForm()
    return render(request, 'HRindex.html', {'form': form})

My HTML:

{% extends 'base.html' %}

{% block body %}
<div class="jumbotron">
  <h1>Welcome to SoftScores.com</h1>
  <h2>Team analytics platfom</h2>
  <h3>Welcome to {{user.username}}, it is your Page</h3>
</div>
<div class="container">
  <p>
    <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
      Create a new team
    </a>
  </p>
  <div class="collapse" id="collapseExample">
    <div class="card card-body">
      In order to create a new team please invite new members. A link will be sent to them in order to give the access to the application
    </div>
    <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
    </form>
  </div>

</div>

urls.py: from django.conf.urls import url from website import views

app_name = 'website'

urlpatterns = [
    url(r'^candidateIndex/$', views.candidateIndex.as_view(), name='candidate_index'),
    url(r'^HRIndex/$', views.HRIndex.as_view(), name='HR_index'),
]

When it render the page I get only the button but the form does not seems to work Do you habe any idea ?

Upvotes: 0

Views: 571

Answers (1)

Alasdair
Alasdair

Reputation: 308769

You HR_index url is being handled by the HRIndex view, but this does not have any code to handle the form.

url(r'^HRIndex/$', views.HRIndex.as_view(), name='HR_index'),

Since a TemplateView is not really suited to handling a form, it would be better to modify the URL pattern to use the create_invite view instead:

url(r'^HRIndex/$', views.create_invite, name='HR_index'),

Upvotes: 1

Related Questions