I am newbie working with django. I need simple example.
How to submit form (post) without refreshing page using Django, Ajax, jQuery?
This is my form, views and template:
views.py
from django.shortcuts import *
from django.template import RequestContext
from linki.forms import *
def advert(request):
if request.method == "POST":
form = AdvertForm(request.POST)
if(form.is_valid()):
print(request.POST['title'])
message = request.POST['title']
else:
message = 'something wrong!'
return render_to_response('contact/advert.html',
{'message':message},
context_instance=RequestContext(request))
else:
return render_to_response('contact/advert.html',
{'form':AdvertForm()},
context_instance=RequestContext(request))
forms.py (form using "ModelForm")
from django import forms
from django.forms import ModelForm
from linki.models import Advert
class AdvertForm(ModelForm):
class Meta:
model = Advert
TEMPLATES (form html code)
<html>
<head>
</head>
<body>
<h1>Leave a Suggestion Here</h1>
{% if message %}
{{ message }}
{% endif %}
<div>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit Feedback" />
</form>
</div>
</body>
</html>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…