0

I have problem with my code which is i try to submit my Event form in the models. But when i run the form it will show the whole form (and another thing is that it will not show the values in the select box of the rounds) and when i click on the the submit button it will through an error.Please help me out this.

VIEW SIDE CODE:--

def addevents(request):
    if request.method=="POST":
        name=request.POST['events']
        est=request.POST['starttime']
        eet=request.POST['endtime']
        s=Event()
        s.ename=name
        s.event_start_time=est
        s.event_end_time=eet
        s.save()
        cats = request.POST.getlist('cats')
        for i in cats:
            s.categories.add(Category.objects.get(id=i))
        s.save()

        roundd = request.POST.getlist('rround')
        for j in roundd:
            s.rounds.add(Round.objects.get(id=j))
        s.save()
        return render(request,'adminside/addevents.html')
    else:
        rounds = Round.objects.all()
        categories = Category.objects.all()
        return render(request,'adminside/addevents.html',{'categories':categories,'rounds':rounds})

MODELS SIDE:-

class Event(models.Model):
    ename=models.CharField(max_length=200)
    categories = models.ManyToManyField(Category)
    event_data = models.DateField()
    event_start_time = models.TimeField()
    event_end_time = models.TimeField()
    rounds = models.ManyToManyField(Round)

EVENT PAGE FORM:-

{% extends 'adminside/master.html' %}
{% block content %}
<div class="col-12">
        <div class="card">
          <div class="card-body">
            <h4 class="card-title">Events</h4>
            <p class="card-description"> All fields are Compulsory </p>
            <form class="forms-sample" method="POST">
              {% csrf_token %}
              <div class="form-group">
                <label for="exampleInputEmail1">Add Event</label>
                <input type="text" class="form-control" name="events" id="exampleInputEmail1" placeholder="Enter Event">
              </div>
              <div class="form-group">
                <label>Categories:</label>
                <select class="form-control" multiple name="cats">
                  {% for i in categories %}
                  <option value="{{ i.id }}">{{ i.cname }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="form-group">
                <label for="exampleInputEmail1">Event Start Time</label>
                <input type="text" class="form-control" name="starttime" id="exampleInputEmail1" placeholder="Enter Event Start Time">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail1">Event End Time</label>
                <input type="text" class="form-control" name="endtime" id="exampleInputEmail1" placeholder="Enter Event End Time">
              </div>
              <div class="form-group">
                <label for="exampleInputEmail1">Round Name</label>
                <select class="form-control form-control-lg" id="exampleFormControlSelect1" multiple name="rround">
                    {% for i in rounds %}
                    <option value="{{i.id}}">{{i.round}}</option>
                    {% endfor %}
                      </select>
              </div>
              <button type="submit" value=Addme class="btn btn-success mr-2">Submit</button>
              <button class="btn btn-light">Cancel</button>
            </form>
          </div>
        </div>
      </div>
{% endblock %}
Encaledus
  • 51
  • 9

1 Answers1

0

in addevents view you need to add something in event_data before saving because this value do not accept null or modify your models.py

event_data = models.DateField()

modify this to default value like the current time

event_data = models.DateField(default=timezone.now)

hope this helps