javascript - Django: Jquery click function not working in Ajax -
i have been working through tango django exercises cut teeth django. done having problem ajax part.
ajax function auto_add page not being called. idk problem since other functions being called. on shell prompt, there no call ajax function @ all. needed.
pertinent code attached. same on website link above. static/rango-ajax.js
$('.rango-add').click(function(){ var catid = $(this).attr("data-catid"); var title = $(this).atrr("data-title"); var url = $(this).attr("data-url"); $.get('/rango/auto_add_page/', {category_id: catid, url: url, title: title}, function(data){ $('#pages').html(data); me.hide(); }); });
templates/rango/category.html
{% if user.is_authenticated %} <button data-catid="{{category.id}}" data-title="{{ result.title }}" data-url="{{ result.link }}" class="rango-add btn btn-mini btn-info" type="button">add</button> {% endif %}
rango/views.py
@login_required def auto_add_page(request): context = requestcontext(request) cat_id = none url = none title = none context_dict = {} if request.method == 'get': cat_id = request.get['category_id'] url = request.get['url'] title = request.get['title'] if cat_id: category = category.objects.get(id=int(cat_id)) p = page.objects.get_or_create(category=category, title=title, url=url) pages = page.objects.filter(category=category).order_by('-views') #adds our results list template context under name pages. context_dict['pages'] = pages return render_to_response('rango/page_list.html', context_dict, context)
rango/urls.py
urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^goto/$', views.track_url, name='track_url'), url(r'^add_category/$', views.add_category, name='add_category'), url(r'^auto_add_page/$', views.auto_add_page, name='auto_add_page'),
complete code @ link.
your code good, thing have define template in /tango/templates/rango/page_list.html. template have following code:
{% if pages %} <ul> {% page in pages %} <li> <a href="{% url 'goto' %}?page_id={{page.id}}"> {{ page.title}}</a> {% if page.views > 1 %} ({{page.views}} views) {% elif page.views == 1 %} ({{page.views}} view) {% endif %} </li> {% endfor %} </ul> {% else %} <strong> no pages in category. </strong> {% endif %}
and inside of category template must define following code:
% if category %} {% if user.is_authenticated %} <a href="/rango/category/{{ category_name_url }}/add_page/"> add new page</a> <br> {% endif %} {% if pages %} <div id="pages"> <ul> {% page in pages %} <li> <a href="{% url 'goto' %}?page_id={{page.id}}"> {{ page.title}}</a> {% if page.views > 1 %} ({{page.views}} views) {% elif page.views == 1 %} ({{page.views}} view) {% endif %} </li> {% endfor %} </ul> </div> {% else %} <strong> no pages in category. </strong> {% endif %} {% else %} specified category {{ category_name }} not exist! {% endif %}
Comments
Post a Comment