2012-07-12 103 views
0

我试图用this link创建一个Django Rest API,但我得到一个无效的语法错误。这里是我的代码: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/

错误 的SyntaxError在/ XML /调查/ 无效的语法(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) 
+0

你有没有一个右括号太多?在收集(...) – KCiebiera 2012-07-12 19:04:14

+0

谢谢..它得到解决,但现在我得到这个错误 - 没有模块名为django_restapi.model_resource – user1050619 2012-07-12 20:59:12

回答