No reverse match error while using Jquery with Django . How to debug?
I am on the learning phase of django and have encountered a weird bug while doing so .
I am using Jquery to supply the front end with the list of users registered .
My template looks something like this
<html>
<head>
<title>Userbase</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="/media/js/autocomplete.css">
<script type="text/javascript" src="/media/js/jquery-1.2.1.js"></script>
<script type="text/javascript" src="/media/js/dimensions.js"></script>
<script type="text/javascript" src="/media/js/autocomplete.js"></script>
{% block extra_css %}{% endblock extra_css %}
</head>
<script type="text/javascript" >
$( document ).ready( function() {
$( '#searchSubmit' ).click( function() {
q = $( '#q' ).val();
$( '#results' ).html( ' ' ).load(
'{% url userbase_user_search %}?q=' + q );
});
});
$( document ).ajaxStart( function() {
$( '#spinner' ).show();
}).ajaxStop( function() {
$( '#spinner' ).hide();
});
</script>
<label for="">Users: </label>
<input type="text" id="UserSearchField" name="UserSearchField">
My views look something like this
def ajax_user_search( request ):
if request.is_ajax():
q = request.GET.get( 'q' )
if q is not None:
results = User.objects.filter(
Q( first_name__contains = q ) |
Q( last_name__contains = q ) |
Q( username__contains = q ) ).order_by( 'username' )
template = 'usersearch.html'
data = {
'results': results,
}
return render_to_response( template, data,
context_instance = RequestContext( request ) )
I would be honest , that I got the small jquery code from another code base . So finding it a bit problematic to make it work here . Any help would be much appreciated . All I want is a text field which when clicked , (when the cursor comes on ) , gives a pull down of all the users present in the db .
Any help would be much appreciated . Any tutorial links to understanding jquery would be good too .
Edit:
This is my urls.py
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib import databrowse
from world.views import welcome
from openmaps.views import *
from django.contrib.auth.views import login, logout
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', welcome),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^databrowse/(.*)', databrowse.site开发者_开发百科.root),
url(r'^static/(?P<path>. *)$', 'django.views.static.serve', {
'document_root': 'q:\projects\cape\static', 'show_indexes': True}),
url(r'^accounts/', include('registration.urls')),
url(r'^userbase/','userbase.views.PermLayer',name='usersearch'),
)
That is how it looks like now . PermLyaer is one of the classes of the view .
{% url userbase_user_search %}
That gets the url with the name userbase_user_search but you don't have a url with that name.
I am also trying to solve this issue in the same ajax application. You can simply replace the below code in index.html under your base.html
. If you have used different name from demo_user_search
in urls.py
replace it with your name.
$( '#results' ).html( ' ' ).load( "{% url 'demo_user_search' %}?q=" + q );
or (Without quotes in load()
function
$( '#results' ).html( ' ' ).load( {% url 'demo_user_search' %}?q= + q );
精彩评论