IPO
IPO

Reputation: 835

Django Form Not Submitting From Template

I am having troubles with submitting the this form. I could submit the form from the admin dashboard but not from the template. What could I be doing wrong?

models.py file:

from teamprofile.models import TeamProfile
from django.utils import timezone
from competition.models import CompetitionProfile
from datetime import datetime
from django.contrib.auth.models import User

class MatchProfile(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    competition = models.ManyToManyField(CompetitionProfile)
    home_team = models.ForeignKey(TeamProfile, on_delete=models.CASCADE, related_name = 'home_team')
    away_team = models.ForeignKey(TeamProfile, on_delete=models.CASCADE, related_name = 'away_team')
    # start_time = models.DateField(blank=True, null=True)



    def __str__(self):
        return "{} vs {}".format(self.home_team, self.away_team)

forms.py file:

from .models import MatchProfile

class CreateMatchForm(forms.ModelForm):


class Meta():
    model = MatchProfile
    fields = ('competition', 'home_team', 'away_team')
    widgets = {
        
        'competition': forms.Select(attrs={'class':  'uk-select'}),
        'home_team': forms.Select(attrs={'class':  'uk-select' }),
        'away_team': forms.Select(attrs={'class':  'uk-select' }),
        # 'start_time': forms.SelectDateWidget(),

        
        
    }

views.py file:

from .forms import CreateMatchForm
from .models import MatchProfile

class MatchProfileCreateView(CreateView):
    model = MatchProfile
    success_url = reverse_lazy('home')
    form_class = CreateMatchForm


    def form_valid(self, form):
        form.instance.owner = self.request.user

        return super(MatchProfileCreateView, self).form_valid(form)

.html file:

                            <form class="uk-child-width-1-2@s uk-grid-small p-4" uk-grid method="POST">
                                <div> {% csrf_token %}
                                    <h5 class="uk-text-bold mb-2">  Competiton Name </h5>
                                    {{ form.competition }}
                                    <!-- <input type="text" class="uk-input" placeholder="Your name"> -->
                                </div>
                                <div>
                                    <h5 class="uk-text-bold mb-2">  Home Teams </h5>
                                    {{ form.home_team }}
                                    <!-- <input type="text" class="uk-input" placeholder="Your seccond"> -->
                                </div>
                                <div>
                                    <h5 class="uk-text-bold mb-2">   Away Team </h5>
                                    {{ form.away_team }}
                                    <!-- <input type="text" class="uk-input" placeholder="[email protected]"> -->
                                </div>
                                <!-- <div>
                                    <h5 class="uk-text-bold mb-2"> Organizer Contact</h5>
                                    {{ form.organizer_contact }} -->
                                    <!-- <input type="text" class="uk-input" placeholder="+1 555 623 568 "> 
                                </div> -->
                            

                                <div class="uk-flex uk-flex-right p-4">
                                    <button class="button soft-primary mr-2">Cancel</button>
                                    <button type="submit" class="button primary">Create Match</button>
                                
                                </div>
                            </form>

I am definitely doing something the wrong way but I can't seem to figure it out. Other forms built with same method and template seems to be working.

My terminal would show that a POST request was made yet it still doesn't work

Upvotes: 0

Views: 162

Answers (1)

IPO
IPO

Reputation: 835

I would find that error was from forms.py:

Turns out the POST request send a field input different from what was declared on the model.

I changed the

'competition': forms.Select(attrs={'class': 'uk-select'}),.

to

'competition': forms.SelectMultiple(attrs={'class':  'uk-select'}),

Difference being forms.SelectMultiple as the model was a ManyToManyField.

Upvotes: 1

Related Questions