django ajax runtime error - URL doesn't end in slash

1,006 views
Skip to first unread message

Robin Lery

unread,
Aug 10, 2013, 1:46:42 PM8/10/13
to django...@googlegroups.com

I am practicing from a tutorial where I have reached to create a search box with jquery and ajax. Every thing is going good, except, when i press any key in the search, I get an error,

RuntimeError at /articles/search You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to localhost:8000/articles/search/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

I checked for "/" in my code, but its there. Don't know what's going on. Please help.

application's urls.py:

    url(r'^search/$', 'article.views.search_title'),
)

views.py:

def search_title(request):
    if request.method == "POST":
        search_text = request.POST['search_text']
    else:
        search_text = ''

    articles = Article.objects.filter(title__contains=search_text)

    return render_to_response('ajax_search.html', {'article': article})

I'm using jquery version: jquery-2.0.0.min.js

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 searchSuccess(data, textStatus, jqXHR)
{
    $('#search-results').html(data);
}

And even when I inspect the ajax.js, in the last line

$('#search-results').html(data);

It reads it as:

$('#search-results').html(date);

Please help me somebody. Thank you.

My main url:

(r'^articles/', include('article.urls')),

Jonathan D. Baker

unread,
Aug 10, 2013, 1:52:12 PM8/10/13
to django...@googlegroups.com
Can you paste the markup for your search form?

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Robin Lery

unread,
Aug 10, 2013, 2:05:30 PM8/10/13
to django...@googlegroups.com
Ok! Here it is:

{% extends "base.html" %}

{% block sidebar %}
    <ul>
        <li><a href="/articles/all">Articles</a></li>
        <li><a href="/articles/create/">Create Article</a></li>
    </ul>

    <h3>Search</h3>
    {% csrf_token %}
    <input type="text" id="search" name="search" />

    <ul id="search-results">

    </ul>
{% endblock %}

{% block content %}

<h2>Language is: {{language}}</h2>
<h2>Session Language is: {{session_language}}</h2>

{% if articles.count > 0 %}
{% for article in articles %}
<div>
<h2><a href="/articles/get/{{article.id}}/">{{article.title}}</a></h2>
<p>{{article.body | lower | truncatewords:"10"}}</p>
<p>{{ article.likes }} people liked this article.</p>

</div>
{% endfor %}
{% else %}
<p>None to show.</p>
{% endif %}

{% endblock %}
Reply all
Reply to author
Forward
0 new messages