Home > Enterprise >  form associated with the model not submitting to database
form associated with the model not submitting to database

Time:08-10

I'm newbie in the Python. I made a landing page that has a form with sending a phone number, but it does not get into the database. Form associated with the model

Models.py:

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField


class CallOrder(models.Model):
    first_name = models.CharField(max_length=50, blank=True, verbose_name='First name')
    phone = PhoneNumberField(null=False, blank=False, unique=False, verbose_name='Phone')
    email_field = models.EmailField(blank=True, verbose_name='Email')
    message = models.TextField(max_length=400, blank=True, verbose_name='Comments')
    created_at = models.DateTimeField(auto_now=True, verbose_name='Created at')

    class Meta:
        verbose_name = 'Call order'
        verbose_name_plural = 'Call orders'

Forms.py (CallOrderForm):

from django import forms
from repair_service.models import CallOrder

    class CallOrderForm(forms.ModelForm):
        class Meta:
            model = CallOrder
            fields = ['first_name', 'phone', 'email_field', 'message']
            widgets = {
                'first_name': forms.TextInput(attrs={'class': 'form-control'}),
                'phone': forms.TextInput(attrs={'class': 'form-control'}),
                'email_field': forms.TextInput(attrs={'class': 'form-control'}),
                'message': forms.Textarea(attrs={'class': 'form-control', 'rows': '3'}),
            }

Views.py:

from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import CallOrderForm


def base(request):
    header_menu_button = {'': 'Home', 'qui_sommes_nous': 'Qui sommes nous', 'nos_services': 'Nos services', 'avis': 'Avis', 'contacts': 'Contacts', 'faq': 'FAQ', 'partenaires': 'Partenaires'}
    content_body = {'qui_sommes_nous': 'qui_sommes_nous_content', 'nos_services': 'nos_services_content', 'avis': 'avis_content', 'contacts': 'contacts_content', 'faq': 'faq_content', 'partenaires': 'partenaires_content'}
    form = CallOrderForm()
    return render(request=request, template_name='repair_service/base.html', context={'title': 'Mr.Ginzby', 'header_menu_button': header_menu_button, 'content_body': content_body, 'form': form})

def call_back(request):
    if request.method == 'POST':
        form = CallOrderForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/thanks/')
    else:
        form = CallOrderForm()
    return render(request, 'repair_service/base.html', {'form': form})

Html:

<form action="" method="post">
    {% csrf_token %}<br>
    {{ form.as_p }}
    <div ><input type="submit" value="Submit" ></div>
</form>

CodePudding user response:

Django always takes current page route, form's data is not even goes to call_back view, one way to solve the problem is to specify the url name in the action attribute of form, so that the data could go to call_back view, but I have another approach of making single view instead of two views, so:

views.py:

def base(request):
    context='' # for the error local variable `context` referenced before assignment.
    if request.method == 'POST':
        form = CallOrderForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/thanks/')
    else: # Get condition
        header_menu_button = {'': 'Home', 'qui_sommes_nous': 'Qui sommes nous', 'nos_services': 'Nos services', 'avis': 'Avis', 'contacts': 'Contacts', 'faq': 'FAQ', 'partenaires': 'Partenaires'}
        content_body = {'qui_sommes_nous': 'qui_sommes_nous_content', 'nos_services': 'nos_services_content', 'avis': 'avis_content', 'contacts': 'contacts_content', 'faq': 'faq_content', 'partenaires': 'partenaires_content'}
        form = CallOrderForm()
        context={'title': 'Mr.Ginzby', 'header_menu_button': header_menu_button, 'content_body': content_body, 'form': form}
        
    return render(request, 'repair_service/base.html', context)

Remove that call_back view.

  • Related