sumit4613
sumit4613

Reputation: 69

Object create via ModelForm does not show up in List

I created a model and just trying to store the details using the ModelForm in the backend but i'm unable to do it as whenever I try to submit my form it always shows else part of my views.py that is -> Please try again.

If someone knows what i'm doing wrong please let me know.

P.S -> I have two form methods in my templates.html but I haven't wrote any code for that now, just trying to save that first one now.

I tried to solve this problem by looking for some answers here but didn't got any success.

models.py

class OrgMember(models.Model):
    org_poc = models.CharField(max_length=100, blank=False, verbose_name="Organization POC")
    org_name = models.CharField(max_length=100, blank=False, verbose_name="Organisation Name")
    phone = models.CharField(max_length=10, blank=False, verbose_name="Phone Number")
    email = models.EmailField(blank=False, unique=True, verbose_name="Email ID")

    def __str__(self):
        return self.email

forms.py

class OrgMembersForm(forms.ModelForm):
    class Meta:
        model = OrgMember
        fields = ['org_poc', 'org_name', 'phone', 'email']

views.py

def org_member_view(request):
    if request.method == "POST":
        form = OrgMembersForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Member Added Successfully.")
            return redirect('users-members')
        else:
            messages.error(request, "Please Try Again.")
    else:
        form = OrgMembersForm()
    members = OrgMember.objects.all()
    context = {'form': form, 'members': members}
    return render(request, 'users/members.html', context)

template.html

The table in which I want my data

  <table class="table table-responsive">
            <thead>
              <tr>
                <th>#</th>
                <th class="th-lg">Organization POC</th>
                <th class="th-lg">Organization Name</th>
                <th class="th-lg">Phone</th>
                <th class="th-lg">Email</th>
              </tr>
            </thead>
            <tbody>
              {% for member in members %}
              <tr>

                <th scope="row">{{ forloop.counter }}</th>
                <td>{{ member.org_poc }}</td>
                <td>{{ member.org_name }}</td>
                <!--              <td>Table cell</td>-->
                <td>{{ member.phone }}</td>
                <td>{{ member.email }}</td>

              </tr>
              {% endfor %}
            </tbody>
          </table>

Form Method

<form method="POST" action="" enctype="multipart/form-data">
      {% csrf_token %}
      <div class="card">

        <div class="card-body">
          <!--Header -->
          <!--Body -->
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Organization POC</label>
          </div>
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Organization Name </label>
          </div>
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Phone</label>
          </div>

          <div class="md-form">
            <i class="fas fa-envelope prefix grey-text"></i>
            <input type="text" id="form-email" class="form-control">
            <label for="form-email">Email </label>
          </div>
        </div>
      </div>
      <!--Form with header -->

      <!-- Footer -->
      <div class="modal-footer justify-content-center">
        <button type="submit" class="btn btn-success">Submit <i class="far fa-gem ml-1"></i></button>
        <a type="button" class="btn btn-outline-success waves-effect" data-dismiss="modal">Cancel</a>
      </div>
    </form>

Upvotes: 0

Views: 60

Answers (1)

Risadinha
Risadinha

Reputation: 16666

You form is invalid on submit but you never output error messages or log output.

Check the documentation on how to output error messages inside the form in the template: https://docs.djangoproject.com/en/2.2/topics/forms/#rendering-fields-manually

Or - at least - add log output to this part in your code:

import logging
log = logging.getLogger(__name__)

# .... further down
    else:
        messages.error(request, "Please Try Again.")
        log.error("form is invalid: %s", form.errors)

Though, I'd recommend adding it to the form so that your user are informed when the form is invalid.

Upvotes: 1

Related Questions