0

我正在尝试使用此链接创建 Django Rest API ,但出现无效语法错误。这是我的代码:

网址.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),
)

视图.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))     
)   

模型.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/polls/(.*?)/?$',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)
4

1 回答 1

2

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

于 2012-12-16T23:02:33.103 回答