Friday, June 14, 2019

Re: Create View not saving form data to database

I found the error. I didn't had a required field for my model present in my html. so I was submitting blank to a field that required and input.

On Friday, 14 June 2019 07:04:38 UTC-4, Joe Reitman wrote:
Your passing strings as the first argument in your many-to-many fields. Shouldn't they be classes? Doc

On Thursday, June 13, 2019 at 8:31:29 PM UTC-5, curtly critchlow wrote:
CreateView class not saving data to database. When I hit submit, the html the page only refreshes. l'm not getting any errors so I have no idea what i'm doing wrong. Does CreateView automatically takes care of many to many relationships? here is my code below, I've been trying to solve this problem 2 days now.

  1. Models.py
  2.  
  3. from django.db import models
  4. from django.utils import timezone
  5. from django.urls import reverse_lazy, reverse
  6. # Create your models here.
  7. class Requisition(models.Model):
  8.    # constants
  9.    FROZEN = 'Frozen'
  10.    CHILLED = 'Chilled/Cold Pack'
  11.    DRYICE = 'Dry Ice'
  12.    ROOMTEMP = 'Room Temperature'
  13.    TRANSPORT_CHOICE = [(FROZEN,FROZEN),(CHILLED,CHILLED),(DRYICE,DRYICE),(ROOMTEMP,ROOMTEMP)]
  14.    
  15.    EMAIL = 'email'
  16.    HARDCOPY = 'Hardcopy Pickup'
  17.    BOTH = 'Email & Hardcopy Pickup'
  18.    RECEIPT_CHOICE = [(EMAIL,EMAIL), (HARDCOPY,HARDCOPY), (BOTH,BOTH)]
  19.    
  20.     ACUTE = 'Acute'
  21.    CHRONIC = 'Chronic'
  22.    SYMPTOMS_CHOICE = [(ACUTE,ACUTE,), (CHRONIC,CHRONIC)]
  23.     #columns
  24.    accession_number = models.CharField(max_length=254, unique=True, help_text='format: VSLXXX-XXX')
  25.    customer = models.ForeignKey('general.Customer', on_delete=models.CASCADE, related_name='requisition')
  26.    
  27.    # Veterinary Officer Info
  28.    veterinary_officer = models.CharField(max_length=50, blank=True)
  29.    address = models.CharField(max_length=50, blank=True)
  30.    contact_number = models.CharField(max_length=13, blank=True, help_text="number format example: +592 XXX XXXX")
  31.     email_address = models.EmailField(max_length=50, blank=True)
  32.     # General sample info
  33.    
  34.    collected_by = models.CharField(max_length=50)
  35.    time_of_collection = models.DateTimeField()
  36.    
  37.     method_of_transport = models.CharField(max_length=50, choices=TRANSPORT_CHOICE)
  38.    result_transmission = models.CharField(max_length=50, choices=RECEIPT_CHOICE)
  39.     #pathology
  40.    reason_for_request = models.ManyToManyField('ReasonForRequest',related_name='requisition', blank=True)
  41.    illness_date = models.DateField('Date of onset of illness', blank=True, null=True)
  42.    illness_duration = models.CharField('Duration of illness', max_length=254, blank=True)
  43.    symptoms = models.CharField(max_length=50, choices=SYMPTOMS_CHOICE, blank=True)
  44.    fever = models.BooleanField('Presence of Fever', blank=True, null=True)
  45.    body_temp = models.PositiveSmallIntegerField('Max body Temperature (C)', blank=True, null=True)
  46.    fever_duration = models.CharField('Duration of Fever',max_length=50, blank=True)
  47.    signs = models.ManyToManyField('Signs', related_name='requisition', blank=True)
  48.    vaccination_status = models.CharField(max_length=50, blank=True)
  49.    vaccination_date = models.DateField('Date of Vaccination', blank=True, null=True)
  50.    treatments = models.TextField('Previous/Current Treatments', blank=True)
  51.    exposed = models.ManyToManyField('Exposed', related_name='requisition', through='ExposureHistory', blank=True)
  52.    
  53.    comments = models.TextField(blank=True)
  54.    recieved_by = models.ForeignKey('auth.User', on_delete=models.CASCADE)
  55.    submitted_by = models.CharField(max_length=254, help_text='if multiple persons separate names using "&" exampe: John Doe               & Jane Doe')
  56.    date_of_submission = models.DateTimeField(auto_now_add=True)
  57.     def __str__(self):
  58.        return self.accession_number
  59.  
  60.    def get_absolute_url(self):
  61.        return reverse("laboratory:requisition_details", kwargs={'pk':self.pk})
  62.    
  63. forms.py
  64. from django.forms import ModelForm
  65. from general.models import Customer
  66. from laboratory.models import Requisition
  67. from bootstrap_datepicker_plus import DatePickerInput, DateTimePickerInput
  68. class RequisitionForm(ModelForm):
  69.    class Meta:
  70.        model = Requisition
  71.        fields = '__all__'
  72.        widgets = {
  73.            'illness_date': DatePickerInput,
  74.            'vaccination_date': DatePickerInput,
  75.            'date_of_submission': DateTimePickerInput,
  76.        }
  77. views.py
  78. from django.contrib.auth.mixins import LoginRequiredMixin
  79. from django.urls import reverse_lazy, reverse
  80. from django.shortcuts import render
  81. from django.views.generic import TemplateView, CreateView, DetailView, ListView, UpdateView, DeleteView
  82. from general.models import Customer
  83. from laboratory.models import Requisition, Sample
  84. from laboratory.forms import RequisitionForm
  85. # Create your views here.
  86. class RequisitionListView(LoginRequiredMixin, ListView):
  87.    model = Requisition
  88.    context_object_name = 'requisition_list'
  89.    template_name ='laboratory/requisition_list.html'
  90. class RequisitionDetailView(DetailView):
  91.    model = Requisition
  92.    context_object_name = 'requisition_details'
  93.    template_name='laboratory/requisition_details.html'  
  94.  
  95. class RequisitionCreateView(LoginRequiredMixin, CreateView):
  96.    model = Requisition
  97.    form_class= RequisitionForm
  98.    template_name = 'laboratory/requisition_create.html'
  99. requisition_create.html
  100. {% extends "user_home.html" %}
  101. {% load static %}
  102. {% load widget_tweaks %}
  103. {% load bootstrap4 %}      
  104. {% bootstrap_css %}        
  105. {% bootstrap_javascript jquery='full' %}  
  106. {‌{ form.media }}
  107.  
  108. {% block form %}
  109. <div class="main jumbotron">
  110.  <h1>Complete Requisition form below</h1>
  111.  <p></p>
  112.  <form method="POST">
  113.       {% csrf_token %}
  114.      <div class='row'>
  115.        <div class='col'>
  116.          <div class="card">
  117.        <div class="card-header">
  118.          General Information
  119.        </div>
  120.        <div class="card-body fieldwrapper form-group">
  121.          {‌{ form.accession_number.errors }}
  122.          {‌{ form.accession_number.label_tag }}
  123.          {‌{ form.accession_number|add_class:"form-control" }}
  124.          <p></p>
  125.           {‌{ form.customers.errors }}
  126.          {‌{ form.customer.label_tag }}
  127.          {‌{ form.customer|add_class:"form-control" }}
  128.          <p></p>
  129.           {‌{ form.collected_by.errors }}
  130.          {‌{ form.collected_by.label_tag }}
  131.          {‌{ form.collected_by|add_class:"form-control" }}
  132.          <p></p>
  133.           {‌{ form.recieved_by.errors }}
  134.          {‌{ form.recieved_by.label_tag }}
  135.          {‌{ form.recieved_by|add_class:"form-control" }}
  136.          <p></p>
  137.           {‌{ form.submitted_by.errors }}
  138.          {‌{ form.submitted_by.label_tag }}
  139.          {‌{ form.submitted_by|add_class:"form-control" }}
  140.          <p></p>
  141.           {‌{ form.date_of_submission.errors }}
  142.          {‌{ form.date_of_submission.label_tag }}
  143.          {‌{ form.date_of_submission|add_class:"form-control" }}
  144.            
  145.        </div>
  146.          </div>
  147.        </div>
  148.        
  149.        <p></p>
  150.        <div class="col">
  151.         <div class="card">
  152.        <div class='card-header'>
  153.          Veterinarian/Leo/Lea Information
  154.        </div>
  155.        <div class="card-body">
  156.          {‌{ form.veterinary_officer.errors }}
  157.          {‌{ form.veterinary_officer.label_tag }}
  158.          {‌{ form.veterinary_officer|add_class:"form-control" }}
  159.         <p></p>
  160.          {‌{ form.address.errors }}
  161.         {‌{ form.address.label_tag }}
  162.         {‌{ form.address|add_class:"form-control" }}
  163.         <p></p>
  164.          {‌{ form.contact_number.errors }}
  165.         {‌{ form.contact_number.label_tag }}
  166.         {‌{ form.contact_number|add_class:"form-control" }}
  167.         <p></p>
  168.          {‌{ form.email_address.errors }}
  169.         {‌{ form.email_address.label_tag }}
  170.         {‌{ form.email_address|add_class:"form-control" }}
  171.        </div>  
  172.         </div>
  173.         </div>
  174.        
  175.      </div>
  176.       <p></p>
  177.      
  178.      <div class="card">
  179.      <div class='card-header'>
  180.        Transmission Information
  181.      </div>
  182.      <div class="card-body">
  183.        {‌{ form.method_of_transport.errors }}
  184.        {‌{ form.method_of_transport.label_tag }}
  185.        {‌{ form.method_of_transport|add_class:"form-control" }}
  186.        <p></p>
  187.         {‌{ form.result_transmission.errors }}
  188.        {‌{ form.result_transmission.label_tag }}
  189.        {‌{ form.result_transmission|add_class:"form-control" }}
  190.          
  191.    </div>  
  192.      </div>
  193.      <p></p>
  194.      <div class="card">
  195.        <div class='card-header'>
  196.          Pathology Information
  197.        </div>
  198.          <div class="card-body row">
  199.            <div class='col'>
  200.            {‌{ form.reason_for_request.errors }}
  201.            {‌{ form.reason_for_request.label_tag }}
  202.            {‌{ form.reason_for_request|add_class:"form-control" }}
  203.            <p></p>
  204.             {‌{ form.illness_date.errors }}
  205.            {‌{ form.illness_date.label_tag }}
  206.            {‌{ form.illness_date|add_class:"form-control" }}
  207.           <p></p>
  208.            {‌{ form.symptoms.errors }}
  209.           {‌{ form.symptoms.label_tag }}
  210.           {‌{ form.symptoms|add_class:"form-control" }}
  211.           <p></p>
  212.            {‌{ form.fever.errors }}
  213.           {‌{ form.fever.label_tag }}
  214.           {‌{ form.fever|add_class:"form-control" }}
  215.           <p></p>
  216.            {‌{ form.body_temp.errors }}
  217.           {‌{ form.body_temp.label_tag }}
  218.           {‌{ form.body_temp|add_class:"form-control" }}
  219.           <p></p>
  220.            {‌{ form.fever_duration.errors }}
  221.           {‌{ form.fever_duration.label_tag }}
  222.           {‌{ form.fever_duration|add_class:"form-control" }}
  223.           <p></p>            
  224.          </div>
  225.           <div class='col'>
  226.          {‌{ form.signs.errors }}
  227.          {‌{ form.signs.label_tag }}
  228.          {‌{ form.signs|add_class:"form-control" }}<p></p>
  229.           <p></p>
  230.           {‌{ form.vaccination_status.errors }}
  231.          {‌{ form.vaccination_status.label_tag }}
  232.          {‌{ form.vaccination_status|add_class:"form-control" }}
  233.          <p></p>
  234.           {‌{ form.vaccination_date.errors }}
  235.          {‌{ form.vaccination_date.label_tag }}
  236.          {‌{ form.vaccination_date|add_class:"form-control" }}
  237.          <p></p>
  238.           {‌{ form.treatments.errors }}
  239.          {‌{ form.treatments.label_tag }}
  240.          {‌{ form.treatments|add_class:"form-control" }}
  241.          <p></p>
  242.           {‌{ form.exposed.errors }}
  243.          {‌{ form.exposed.label_tag }}
  244.          {‌{ form.exposed|add_class:"form-control" }}
  245.          </div>
  246.        </div>  
  247.      </div>
  248.      <p></p>
  249.      {‌{ form.comments.errors }}
  250.      {‌{ form.comments.label_tag }}
  251.      {‌{ form.comments|add_class:"form-control" }}
  252.      <p></p>
  253.      
  254.      <input type="submit" class='btn btn-primary' value='Submit'>
  255.    
  256.   </form>
  257. </div>
  258.  
  259. {% endblock form %}


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10489963-1f00-48a5-9cee-094d84ea8a50%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment