0

I'm trying to create a Django Rest API using this link, but I get an invalid syntax error. Here is my code:

urls.py

from django.conf.urls.defaults import *
from polls.views import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
     (r'^polls/$',index),
     (r'^polls/(?P<poll_id>\d+)/$',detail),
     (r'^polls/(?P<poll_id>\d+)/results/$',results),
     (r'^polls/(?P<poll_id>\d+)/vote/$',vote),
     (r'^admin/', include(admin.site.urls)),
     (r'^xml/polls/(.*?)/?$',xml_poll_resource),
)

views.py

from django_restapi.model_resource import Collection
from django_restapi.responder import XMLResponder
from django_restapi.responder import *
from django_restapi_tests.polls.models import Poll, Choice

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from polls.models import Poll, Choice
from django.http import Http404
from django.template import RequestContext
import pdb

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    return render_to_response('index.html',{'latest_poll_list': latest_poll_list})

def detail(request, poll_id):
    pdb.set_trace()
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('detail.html', {'poll': p},
        context_instance=RequestContext(request))

def results(request, poll_id):
    return HttpResponse("Hello, world.Youre at the poll index.")    

def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the poll voting form.
        return render_to_response('polls/detail.html', {
            'poll': p,
            'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('results', args=(p.id,)))


xml_poll_resource = Collection(    
    queryset = Poll.objects.all(),    
    permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),    
    responder = XMLResponder(paginate_by = 10))     
)   

Models.py

from django.db import models
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question    

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes  = models.IntegerField()
    def __unicode__(self):
    return self.choice

This application is based on the tutorial that is given in the Django Website. It was working well but started failing once I implemented the logic for rest APIs.

(r'^xml/polls/(.*?)/?$',xml_poll_resource),

The URL that I'm trying: http://127.0.0.1:8000/xml/polls/

Error SyntaxError at /xml/polls/ invalid syntax (views.py, line 49)

Request Method: GET

Request URL: http://127.0.0.1:8000/xml/polls/

Django Version: 1.3.1

Exception Type: SyntaxError

Exception Value: invalid syntax (views.py, line 49)
2
  • don't you have one right bracket too much? in Collection(...) Commented Jul 12, 2012 at 19:04
  • Thanks..It got resolved but now I get this error-No module named django_restapi.model_resource Commented Jul 12, 2012 at 20:59

1 Answer 1

2

Probably not the answer you are looking for, however I recommand you to have a look at this Django REST framwork: django-rest-framework.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.