Kerry Jones
Kerry Jones

Reputation: 21838

Django URL Routing Issue

Very new to Django, so I apologize as I'm sure this has an easy answer.

I have a PHP background, and so I guessing that I am trying to force a structure I am used to, and not one that is native in Django.

Here is my Project's urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('pm.urls', namespace='pm')),
)

Here is my App's urls.py

from django.conf.urls import patterns, url
from pm import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^label/add/$', views.add_label, name='label_add'),
)

I am doing an AJAX Post request to /label/add/, but it's coming back with a 500 error.

This is the views.py:

from django.shortcuts import render
from pm.models import Label
import json

# Create your views here.
def index(request):
    labels_list = Label.objects.order_by('name')
    return render(request, 'pm/index.html', {
        'labels' : labels_list
    })


""" Labels """
def add_label(request):
    if request.is_ajax():
        response = {
            'success': True
       }
    else:
        response = {
            'success': False,
            'error': "Invalid request"
        }

    return json.dumps(response)

Any advise or references would be great.


UPDATE

Here's the first couple of lines from the traceback I am getting:

AttributeError at /label/add/
'str' object has no attribute 'get'

Upvotes: 1

Views: 113

Answers (1)

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

you have to return HttpResponse instead of string:

return HttpReponse(json.dumps(response), content_type='application/json')

Upvotes: 5

Related Questions