I am new to django and try my best to make something very simple. I have a ModelForm for the following model:
class Queries(models.Model): user_id=models.CharField(max_length=200) query=models.CharField(max_length=200)
And I show the user a simple form that will help in the following:
This is what my view.py looks like:
from django.http import HttpResponse from django.shortcuts import get_object_or_404, render from basicapp.models import QueryForm def index(request): form=MyForm() real_form=form.getForm(request) response=form.response return render(request,'basicapp/index.html',{ 'form': real_form, 'response':response, }) class MyForm: response='' def getForm(self,request): form = QueryForm(request.POST) if form.is_valid(): response=form.cleaned_data['query'] form.save() return form
At the moment, I'm trying to use simple things, I take the value in the form request field and try to send it back to the page until I have worked. This is index.html:
<form action=" " method="post">{% csrf_token %} {{ form }} <p>{{response}}</p> <input type="submit" value="Submit" /> </form>
If I could do this, I think the request material would not be so harsh. The form works fine, the data is saved in the database. Only the response line from views.py cannot be restored inside index.html after the form is views.py . Could you help me?
EDIT: Tried in index.html based on Hoff's answer:
<form id="myForm" action=" " method="get">{% csrf_token %} {{ form }} <input type="submit" value="Submit" /> </form> <div id="response"> </div> <script language="JavaScript"> $(document).ready(function() { $("#myForm").submit(function() { </script>
Still out of luck :(
django django-forms modelform
Md. Abdul Munim
source share