I'm facing some issues on Django and for this searchform I am using Django, Ajax and HTML, I haven't found any error and there is no error displayed on the website. So I hope someone could show me my faults.
here is the codes:
models.py
class Article(models.Model):
user = models.ForeignKey(User)
titre = models.CharField(max_length=100, unique=True)
contenu = RichTextField(null=True)
def __str__(self):
return self.titre
views.py
def index(request):
context = {'derniers_articles': Article.objects.all()}
context.update(csrf(request))
populateContext(request, context)
return render_to_response('index.html', context)
def search_titles(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''
articles = Article.objects.filter(titre__contains=search_text)
return render_to_response('ajax_search.html', {'articles':articles})
ajax_search.html
{% if articles.count > 0 %}
{% for article in articles %}
<li>{{ article.titre }}</li>
{% endfor %}
{% else %}
<p>Nothing to do here.</p>
{% endif %}
urls.py
url(r'^search/$', 'article.views.search_titles', name='search'),
index.html
{% csrf_token %}
<input type="text" id="search" name="search"/>
<ul id="search-results"></ul>
ajax.js
$(function(){
$('#search').keyup(function(){
$.ajax({
type: "POST",
url: "/articles/search",
data: {
'search_text' : $('#search').val(),
'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
},
success: SearchSuccess,
dataType: 'html'
});
});
});
function searchSucces(data, textStatus, jqXHR){
$('#search-results').html(data);
}
I'm a beginner at Django, and this form come from this tutorial. Thank you for your time.
Aucun commentaire:
Enregistrer un commentaire