我一直在拉我的头发。按照tastypie网站上的教程后,我按照以下方式设置了tastypie。然而,每当我去任何网址时,我得到一个网址都没有发现404错误。非常感谢任何帮助!
# EXAMPLE URL NOT WORKING: http://mylocalserver/api/bluebird/album/
# MODEL FILE #################################################################
# Django imports
from django.db import models
# Python imports
import datetime
class Album(models.Model):
name = models.CharField(max_length=20)
description = models.TextField(blank=True)
date = models.DateField(default=datetime.date.today())
school = models.CharField(max_length=20)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.name
class Picture(models.Model):
album = models.ForeignKey('Album')
name = models.CharField(max_length=100)
old_name = models.CharField(max_length=100)
picture_location = models.ImageField(upload_to="BlueBird/Pictures/")
active = models.BooleanField(default=True)
def __unicode__(self):
return self.name
# API FILE #################################################################
from tastypie.resources import ModelResource
from models import Album
from models import Picture
class AlbumResource(ModelResource):
class Meta:
queryset = Album.objects.all()
resource_name = 'album'
class PictureResource(ModelResource):
class Meta:
queryset = Picture.objects.all()
resource_name = 'picture'
# URL FILE
from django.conf.urls import patterns, include, url
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
# TastyPie API
from tastypie.api import Api
# BlueBird imports
import BlueBird.views
from BlueBird.api import AlbumResource
from BlueBird.api import PictureResource
# BlueBird API
bluebird_api = Api(api_name='bluebird')
bluebird_api.register(AlbumResource())
bluebird_api.register(PictureResource())
#album_resource = AlbumResource()
#picture_resource = PictureResource()
# for development only - remove in production server and reconfigure settings file if needed
#from django.conf import settings
#from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/$', include(bluebird_api.urls)),
#url(r'^api/$', include(album_resource.urls)),
)
答案 0 :(得分:1)
您的代码很好 - 但您在api上的网址注册上有一个额外的“$”符号。使用方法:
url(r'^api/', include(bluebird_api.urls)),
而不是:
url(r'^api/$', include(bluebird_api.urls)),
然后,网址'/ api / bluebird / album /'正常工作。