DJANGO Rest API无效语法错误

时间:2012-07-12 18:48:06

标签: django

我正在尝试使用this link创建Django Rest API,但是我收到了无效的语法错误。这是我的代码:

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

此应用程序基于Django网站提供的教程。它运行良好,但是一旦我实现了休息API的逻辑,就开始失败了。

(R '^ XML /轮询/(.*?)/?$',xml_poll_resource),

我正在尝试的网址:http://127.0.0.1:8000/xml/polls/

错误     / xml / polls /中的SyntaxError     语法无效(views.py,第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)

1 个答案:

答案 0 :(得分:2)

可能不是您正在寻找的答案,但我建议您查看一下这个Django REST框架:django-rest-framework.org