I am quite new to django and struggling to do 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 am showing the user a simple form that will help in doing the following:
user will ask a question
The question will be processed(a database query will be generated
based on the question)
Then the query result should be shown just beneath the form in the
same page.
This is how my views.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
For now I am trying simple stuffs,I am taking the value in query field of the form and trying to send it back to the page;so far I am failed.
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 query stuffs wont be that tough.The form is working fine,the datas are getting saved in database. Only the response string from views.py could not be retrieved inside index.html after form submission. Can you please help?
EDIT:
Tried following 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() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('GET'),
success: function(response) { // on success..
$("#response").html(response); // update the DIV
}
});
return false;
});
});
</script>
Still no luck :(
views.py
def index(request):
questions=None
if request.GET.get('search'):
search = request.GET.get('search')
questions = Queries.objects.filter(query__icontains=search)
name = request.GET.get('name')
query = Queries.object.create(query=search, user_id=name)
query.save()
return render(request, 'basicapp/index.html',{
'questions': questions,
})
html
<form method="GET">
Question: <input type="text" name="search"><br/>
Name: <input type="text" name="name"><br/>
<input type="submit" value="Submit" />
</form><br/><br/>
{% for question in questions %}
<p>{{question}}</p>
{% endfor %}
What you need is an asynchronous post (ajax), which is easy with jQuery, see this answer for a complete solution: How to POST a django form with AJAX & jQuery
<input type="text" name="query" />
<input type="submit" name="submit" value="Submit" />
you can check if the form was submitted or not (i.e if it's a post request or not):
if 'submit' in request.POST: #you could use 'query' instead of 'submit' too
# do post related task
# add context variables to render post output
# add another context variable to indicate if it's a post
# Example:
context.update({'post_output': request.POST.get('query','')})
...
return render(request, 'index.html', context)
Then in the template, check if context variable post_output exists, if it does show the output:
{% if post_output %}
Output: {{ post_output }}
{% endif %}
In short, the logic is:
Check if a relevant request.POST dict key exists or not in your view.
If the key exists, then it's a post request; add post related context variables and do post related tasks.
Check if any post related context variable is available in the template and if it does, show post related output.
If you don't want to show the output when the page is simply refreshed after a post, pass the request object to the template and do a check like this:
{% if request.POST.submit and post_output %}
Following Hoff's answer...
Add URL attribute to ajax call:
$(document).ready(function() {
$("#myForm").submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('GET'),
url: '/URL-to-ajax-view/',
success: function(response) { // on success..
$("#response").html(response); // update the DIV
}
});
return false;
});
});
Some ajax handler in views.py:
# /URL-to-ajax-view/
def ajax_get_response(request):
if request.method == "GET" and request.is_ajax:
form = QueryForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponse(form.response)
raise Http404
Tried something like that?
Related
when I post data to the database using ajax code I got an unexpected result. I can post data but output showing is wrong. My ajax code not execute properly
views.py
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = request.POST['owner_id']
property_object = Property.objects.get(id=property_id)
owner_object =User.objects.all().get(username=owner)
notification = "Hi {}, {} \n have interested
in your property '{}'".format(property_object.owner, user.first_name, property_object.headline)
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, property=property_object, owner=owner_object)
notifications.save()
return JsonResponse({"msg":"success"})
ajax code
<form action="{% url 'notification' %}" method="post" id="notify_form" >
{% csrf_token %}
<input type="hidden" name="owner_id" value="{{ accomodation.owner }}">
<button type="buttonsn" id="request-btn" name="property_id" value="{{ accomodation.id }}" class="btn btn-primary">Contact info:</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
$(document).ready(function(e){
$(document).on('submit','#request-btn',function(e){
e.preventDefault();
var property_id = $this.attr('value');
var owner_id = $this.attr('value');
$.ajax({
type:'POST',
url:"{% url 'notification' %}",
data:$('#notify_form').serialize(),
dataType:'Json',
success:function(data){
if(data.msg == "success"){
alert('data submitted')
}
}
});
});
});
I'm expecting a success alert box, but got
{"msg": "success"}
in a white window like 'httpResponse'. ajax code doesn't work
for overcome i'am try somechanges in mycode
i removed action address from form
<form action="" method="post" id="notify_form" >
but it doesn't work this time form not submitted
what can I do
You're attaching the event handler to the button:
$(document).on('submit','#request-btn',function(e){
But buttons don't have submit events, forms do. You can attach it to the form:
$(document).on('submit','#notify_form',function(e){
Or use the button's click event instead:
$(document).on('click','#request-btn',function(e){
Note also that there's a typo in the type attribute for the <button> element. It should be:
type="button"
Ok, so i have in my views.py these functions
def main(request):
if request.method == 'POST':
return newFunction(request)
return render(request,'user/main.html')
def newFunction(request):
num = request.POST["number"]
let = request.POST["letter"]
context = {'num':num,'let':let}
return render(request,'user/test.html',{'c':context})
And that main.html looks like this
{%extends 'base/base.html'%}
{%block title%}Main{%endblock%}
{%block content%}
<form method="post">
{%csrf_token%}
<input type="text" name="number">
<input type="text" name="letter">
<button type="submit">Save</button>
</form>
{%endblock%}
The urls.py
path('index/',main,name='main'),
path('test/',newFunction,name='test'),
As you can see, i enter the main.html page and a form appears, asking me for a number and a letter. I enter these 2 inputs and when clicking the submit, it redirects me to another html and show me the letter and number. The problem is that the url still shows user/index.html, not user/test.html
How can i do this? i want to basically click the submit and send all the values to another template but changing the url in the process.
You can use ajax for do that. You can use form either, but ajax is more appropriate for just passing data to view.
Just sending ajax request with some data, and send it to your target view.
$.ajax({
type: 'POST',
url: "{% url 'test' %}",
data: {
"csrfmiddlewaretoken": "{{ csrf_token }}",
"num": 'number',
"let": 'letter'
},
dataType: 'json',
success: function(e) {
// success after receive response
}
});
Of course you can send it with form. Just add your target url in action.
<!-- add your target url in action -->
<form method="post" action="{% url 'test' %}">
{%csrf_token%}
<input type="text" name="number">
<input type="text" name="letter">
<button type="submit">Save</button>
</form>
This can be handled on the frontend, using the action attribute within the opening <form> tag whose value should be the URL you want the form's data to be POST'd to on submit. Your opening <form> tag can be changed to the following:
<form method="post" action="/user/test.html">
Read: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data
I have some common question about http get vs post,It would be great if someone can clarify with examples...
Examples:-
Here is my view
views.py
def search(request):
if request.method == 'POST':
print 'request.post=', request.POST
print 'request.get=', request.GET
print 'request.method=', request.META.get('REQUEST_METHOD')
if 'q' in request.GET:
message = 'You searched for :%r' % request.POST['q']
else:
message = 'You submitted an empty form'
return render(request, 'search_form.html', {'message': message})
return render(request, 'search_form.html')
search_form.html
<body>
{% if message %}
<div> {{ message }} </div>
{% else %}
<form action="/polls/search/" method="post">{% csrf_token %}
<input type="text" name="q">
<input type="submit" value="Search">
</form>
{% endif %}
</body>
Will POST request used only with the HTML forms? If not, what are the other ways to do it?
Any request without a form is always a GET request?
How to handle GET and POST using a form..
Thanks.
Will POST request used only with the HTML forms? If not, what are the
other ways to do it?
You can post json or xml too. For example, Jquery uses ajax post of json or xml data type
Any request without a form is always a GET request?
There are 4 types of http methods, GET,POST, DELETE and PUT. Depending on how you want to use it, you can use any of them.
An article more on this. http://bradley-holt.com/2009/07/html-5-http-methods-rest/
.
I have a form which allows users to select several parameters to allow faceted querying of data. As there is no data entry going on here I want the form to post to GET and I have a another view with a different template which displays the results.
I want the form to validate as normal so that if a required field is not completed the corresponding errors are displayed. At the moment my process looks like this (simplified):
my search view:
def search(request):
...
context['form'] = GraphForm()
...
return render(request, 'search.html', context)
my results view:
def results(request):
if 'submit' in request.GET:
# process GET variables as query
...
return render(request, 'results.html', context)
my search.html template:
<form action="{% url results %}" method="get">{% csrf_token %}
{% for field in form %}
<div class="field_wrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" name="submit" value="Query" />
</form>
Given that the form submits to another url with separate view code, what is the best way to go about validating (highlighting errors), and ensuring I have my GET data?
Any help much appreciated.
This might be a little late, but I think the following will work while maintaining similarity to 'POST' workflow:
Instead of having two different views for searching and displaying results, just have one view. The normal codepath described for post forms can then be followed. Instead of using request.method == 'POST' to detect form submission, we instead use 'submit' in request.GET. If using javascript to submit the form, make sure that 'submit' is included in the GET data or use a hidden field to detect form submission.
views.py
def search(request):
context_dict = {}
if 'submit' in request.GET:
form = GraphForm(request.GET)
if form.is_valid():
#do search and add results to context
#If you don't want to use a single view,
# you would redirect to results view here.
results = get_results(**form.cleaned_date)
context_dict['results'] = results
else:
form = GraphForm()
context_dict['form'] = form
return render(request, 'search.html', context_dict)
search.html
<form action="{% url 'search' %}" method="get">
{{form}}
<input type="submit" name="submit" value="Query" />
</form>
{% if results %}
{% include 'results.html' %}
{% endif %}
You should be able to pass request.GET just like request.POST to the form. The form simply accepts a data dictionary. It doesn't care where that comes from. Have you already tried that?
Use JavaScript/jQuery for form validation. All you need to do is add an id to form, and in the corresponding Javascript, do something like
document.getElementById("#form").onsubmit = checkForm();
or using jQuery
$("#form").submit(checkForm);
where checkForm() returns true upon successful validation, and false otherwise. (Note that, if you do not return false, form submission will continue as usual.)
Which fields you check for/validate can also change by using Django's templates.
I want to implement a ajax commenting system.
urls.py:
(r'^comment/(\d+)/$', comments),
views.py:
def comments(request,feed):
if request.method == 'POST':
feed=Feed.objects.get(pk=feed)
form = CommentForm(request.POST)
if form.is_valid():
comment, created = Comment.objects.get_or_create(
feed=feed,
msg=form.cleaned_data['msg'],
ip=request.META['REMOTE_ADDR']
)
comments=Comment.objects.filter(feed=feed)
form=CommentForm()
variables=RequestContext(request,{'comments': comments,'feed': feed,'form':form,})
if 'HTTP_REFERER' in request.META:
return HttpResponseRedirect(request.META['HTTP_REFERER'])
return render_to_response('comment_page.html', variables )
#return HttpResponseRedirect('/view/')
else:
form=CommentForm()
feed=Feed.objects.get(pk=feed)
comments=Comment.objects.filter(feed=feed).reverse()
variables=RequestContext(request,{'comments': comments,'feed': feed,'form':form,})
return render_to_response('comment_page.html', variables )
Templates:
<div id="commentbox" style="display:none;">
<form class="comment" method="post" action="/comment/{{feed.id}}/">
{{cform.as_p}}
<input class="post" type="submit" value="post" />
</form>
</div>
</br>
<h3></h3><button class="ccc">Show/Hide Comment</button> {{feed.comment_set.count}} Comments
<div id="commentlist" class="commentlist" style="padding-left:10px;"><ul style="list-style-type:square;">
{% for c in feed.comment_set.all %}
<li>{{c.msg}}</li>
{% endfor %}
</ul>
</div>
What code should I include to add comments into commentlist li field without page refresh. I am new in ajax. Please help. Thanks
Here's what I would do:
Leave the HTML as it is, as it works for people without JavaScript. In you JavaScript, when the user submits the form, stop it from actually happening:
$('#commentbox form').submit(function(e) {
e.preventDefault();
});
Now, when the button is pressed, prevent the default behavior and submit the form via AJAX:
$('#commentbox form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: $(this).parent().attr('action'),
data: $(this).parent().serialize(),
}).done(function(data) {
alert('The AJAX is done, and the server said ' + data);
});
});