Hi,
I am using Django admin - 2.2.1 and Postgresql as my database. So my problem is I have saved the user entered data in form.py on "POST" Request but it isn't saving in the postgresql database.
Model.py -
class modelForm(models.Model):
CHOICES = [('Y', 'Yes'),
('N', 'No')]
allow = models.CharField( max_length = 4 , choices = CHOICES)
description = models.CharField( max_length=1000)
usingtheproduct = models.CharField( max_length = 4 , choices = CHOICES)
Forms.py file -
class productForm(forms.ModelForm):
class Meta:
model = modelForm
fields = ["allow", "description", "usingtheproduct"]
labels = {
"allow": "Allow",
"description": "Description",
"usingtheproduct": "Using The Product"
}
first.html file -
<form name = "form" action = "." method = "POST" >{% csrf_token %}
{{form}}
<input type = "submit", value = "submit">
</form>
Views.py -
def first(request):
template = loader.get_template('xyz/first.html')
form = productForm(request.POST or None) #
https://stackoverflow.com/questions/35748734/django-local-variable-form-referenced-before-assignment if request.method == 'POST':
form = productForm(data = request.POST)
if form.is_valid():
allow = request.POST.get('Allow', '')
decription = request.POST.get('Decription', '')
usingtheproduct= request.POST.get('Using The Product', '')
form.save() # This did not work as did not store user enetered data in postgresql database.
return HttpResponse("It is getting saved!!!!") # I can see this message in browser after submitting the html form,
else:
print(form.errors)
return HttpResponse(template.render({'form': form}, request))
setting.py file -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'abc',
'USER' : 'xyz',
'PASSWORD' : '*********',
'HOST': 'localhost',
'PORT':'',
} # I have also created a database and a table with model name and have the exact columns as that of form input variables that is I have 3 column as allow, description, usingtheproduct in the postgresql database.
}
So not sure why the data is not getting saved in the postgresdatabase, am i missing something? Thanks for your time.
Thanks