Hii Everyone,
I have created a form with 2dropdown list , 1 description box and 1 datetime field but data is not saved in the database. I am getting problem due to date can anyone help me please..
This is my models.py
class data(models.Model):
machinename = models.CharField(max_length=100)
activity = models.CharField(max_length=255)
description = models.CharField(max_length=500)
datetime = models.DateTimeField()
This is my form (html)
{% extends 'main.html' %}
{% load static %}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
{% block content %}
<body bgcolor="white">
<link rel="stylesheet" href="{% static "css/newform.css" %}">
<form name="form" action="newdata" method="POST">
{% csrf_token %}
{{MyDataform.as_p}}
<br><br>
<select name="machinename" id="machinename">
<optgroup label="machinename">
{% for results in data%}
<option>{{results.machinename}}</option>
{%endfor%}
</optgroup>
</select>
<br>
<br>
<select name="activity" id="activity">
<optgroup label="activity">
<option> Idle </option>
<option> Under batch process </option>
<option> Under breakdown </option>
<option> Breakdown raised </option>
<option> Breakdown closed </option>
<option> Under maintenance </option>
<option> Change control raised </option>
<option> Change control closed </option>
</optgroup>
</select>
<br>
<mainlabel> description: </mainlabel><br>
<input type="textarea" placeholder="Enter description" name="description" required><br>
<br>
<input type="datetime-local" required autocomplete="off" name="date" />
<br>
<br>
<button type="submit" class="button">SaveDetails</button>
</form>
{% endblock content %}
</body>
This is my form.py
class Dataform(forms.ModelForm):
class Meta:
model = data
fields = "__all__"
This is my views.py
def newdata(request):
if request.method == "POST":
MyDataform = Dataform(request.POST)
if MyDataform.is_valid():
MyDataform.save()
results = machinelist.objects.all()
#result = data.objects.all()
return redirect('/', {'data': results})
else:
MyDataform=Dataform()
results = machinelist.objects.all()
result = data.objects.all()
return redirect('/',{'data': results},{'record':result})
else:
results = machinelist.objects.all()
result = data.objects.all()
return redirect('/', {'data': results},{'record':result})