django-respite创造对象

时间:2012-10-10 13:06:48

标签: django rest

我正在尝试将django-respite与Django一起使用,但我在创建新对象时遇到了问题。

在正常的Django视图中,我会有

def post_new(request):
    if request.method == 'POST':    
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/posts/")
        else:
            form = PostForm()

    ctx = {'form': form}
    return render_to_response('app_post/new_post.html', ctx, RequestContext(request))

我试图将其移植到喘息之中(该文档仅讨论了GET方法)并且我出来了:

# views.py
class PostViews(Views):
    supported_formats = ['html', 'json']
    template_path     = 'post/'

    def index(self, request):    
        ...

    def show(self, request, id):
        ...

    def create(self, request):
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("posts/")

        return self._render(
            request = request,
            template = 'new_post',
            context = {
                'form': form
            },
       )

    def new(self, request):
        form = PostForm()

        return self._render(
            request = request,
            template = 'new_post',
            context = {
                'form': form
            },
    )


# urls.py
urlpatterns = resource(
    prefix = 'posts/',
    views = PostViews,
    routes = [
        routes.route(
             regex = r'^(?:$|index%s$)' % (templates.format),
             view = 'index',
             method = 'GET',
             name = 'posts'
        ),
        routes.route(
             regex = r'^(?P<id>[0-9]+)%s$' % (templates.format),
             view = 'show',
             method = 'GET',
             name = 'post'
        ),
        # New
        routes.route(
             regex = r'^(?:$|new%s$)' % (templates.format),
             view = 'new',
             method = 'GET',
             name = 'post_new'
        ),
        # Create
        routes.route(
             regex = r'^(?:$|create%s$)' % (templates.format),
             view = 'create',
             method = 'POST',
             name = 'post_create'
        )])

但是当我试着去

http://localhost:8000/posts/new.html 

我得到了

'PostViews' object has no attribute 'new'

有人可以解释我如何通过喘息来管理POST和表单吗?

0 个答案:

没有答案