DjangoRestFramework - 如何自定义前端? [英] DjangoRestFramework - How do I customize the frontend?

查看:754
本文介绍了DjangoRestFramework - 如何自定义前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用DjangoRestFramework基本观点:

This is my basic view using DjangoRestFramework:

class user_list(APIView):
    """
    List all users, or create a new user.
    """
    def get(self, request):
        users = User.objects.all()
        serializer = UserSerializer(users, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = UserSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

当我去调用该视图的URL(本地主机:8000 / CMS /用户),DjangoRestFramework已经有一个前端它说:

When I go to the URL which calls this view (localhost:8000/CMS/users), DjangoRestFramework already has a frontend which says:

GET /CMS/users
HTTP 200 OK
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json

[
    {
        "username": "t",
    }
]

如何定制呢?我想在前端使用AngularJS。我知道,如果没有DjangoRestFramework,我将返回其将设在我的Django应用程序目录下一个名为模板文件夹中的HTML模板。我已经告诉DjangoRestFramework有用becauase我可以创建一个RESTful API,返回JSON对象。我尝试添加以下到我的settings.py文件:

How do I customize this? I want to use AngularJS on the frontend. I know that without DjangoRestFramework, I would return an html template which would be located in my Django App directory in a folder called "Templates". I've been told that DjangoRestFramework is useful becauase I can create a RESTful API which returns JSON objects. I tried adding the following to my settings.py file:

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    )
}

但我仍然似乎无法弄清楚如何我可以自定义我的Django应用程序的前端。我知道有:

but I still can't seem to figure out how I can customize the frontend of my Django application. I know that there is:

renderer_classes = (TemplateHTMLRenderer,)

在我可以在我看来返回一个HTML页面,像这样:

in which I can return an html page in my view, like so:

return Response({'user': self.object}, template_name='user_detail.html')

但不实际返回的HTML页面打败DjangoRestFramework的返回JSON对象,并创建一个RESTful API能力的目的是什么?

but doesn't returning actual HTML pages defeat the purpose of DjangoRestFramework's ability to return JSON objects and create a RESTful API?

推荐答案

首先,有一些事情作出的Django的REST框架具有角发挥得更好。直到我决定尝试烬,我是通过的这个utorial 。要尽量为你的问题提供了一个有用的例子:

First off, there are some things to make Django Rest Framework play better with angular. Until I decided to try out ember, I was working through this utorial. To try to provide a useful example for your question:


  1. 通常情况下,你想要把你DjangoRestFramework REST API中类似/ API。

  1. Typically you want to put your DjangoRestFramework REST Api in something like /api.

如果你正在做看似ubiquitiious单页的应用程序,你就可以有得到的index.html担任了为模板(即只是你的默认主页视图与一个index.html的作为TemplateView模板)

If you're doing the seemingly ubiquitiious single page application, you can then have index.html get served up as a template (i.e. just have your default home view be a TemplateView with index.html as the template)

您将不得不您的角度HTML模板和你的角/ jQuery的/等。 javsascripts和你的CSS文件作为普通的静态文件。注意,你通常不希望有Django模板生成角code。首先,它使事情更是一个痛苦的调试,其次,默认的角度和Django模板语法使用相同的<%字符意味着你必须要小心,以确保一个不尝试跨preT其他模板化code。

You would then have your angular html templates and your angular/jquery/etc. javsascripts and your css files as normal static files. NB that you generally don't want to have django templates generate angular code. First off, it makes things much more of a pain to debug, and secondly, the default angular and django template syntax use the same <% characters meaning you have to be careful to make sure one doesn't try to interpret the others templating code.

在加载的http:// yourserver / ,这将打开index.html页面,然后拉下角的东西,然后开始做基于REST调用您的REST API生成数据。请注意,您可以开始在东西使用Django表单等混合,但我建议你保持简单,然后读取链接的文章(或其他)一旦你有基础去。

When you load http://yourserver/, this will bring up the index.html page which then pulls down angular stuff, which then starts making RESTful calls to your REST api to generate the data. Note that you can start mixing in stuff to use Django forms etc., but I'd recommend keeping it simple and then reading the linked article (or another) once you have the basics going.

这篇关于DjangoRestFramework - 如何自定义前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆