Skip to main content

Django REST_framework APIView

Project description


Use in your app

Install using pip:

pip install django_restframework_apiview

Add ‘apiview’ to your INSTALLED_APPS setting:


JSONP Renderer And debug apiview with paramslist:


Add error code to

    ('ERR_AUTH_NOLOGIN',            10001,  'No login'),
    ('ERR_AUTH_USER_EXISTS',        10002,  'User name has exists'),
    ('ERR_AUTH_USER_NOT_EXISTS',    10003,  'User not exists'),
    ('ERR_AUTH_PASSWORD',           10005,  'Password error'),

edit myapp/

#! usr/bin/env python
# encoding: utf-8
from __future__ import absolute_import, unicode_literals

from django.db import IntegrityError, transaction
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from apiview.err_code import ErrCode
from apiview.exceptions import CustomError
from apiview.view import APIView
from apiview.views import ViewSite
from apiview.views import fields

site = ViewSite(name='base', app_name='base')

class APIBase(APIView):

    class Meta:
        path = '/'
        param_fields = (
            ('channel', fields.CharField(help_text='App Channel', required=False)),
            ('version', fields.CharField(help_text='App Version', required=False)),

class UserAPIBase(APIBase):

    def check_user_permission(self, request):
        user = request.user
        if not user.is_authenticated() or not isinstance(user, User):
            raise CustomError(ErrCode.ERR_AUTH_NOLOGIN)

    def view(self, request, *args, **kwargs):
        return super(APIBase, self).view(request, *args, **kwargs)

    class Meta:
        path = '/user'

class Login(APIBase):

    name = '用户登陆'

    def get_context(self, request, *args, **kwargs):
        user = authenticate(username=request.params.username, password=request.params.password)
        if user is None:
            raise CustomError(ErrCode.ERR_AUTH_PASSWORD)
        login(request, user)
        return self.get_default_context(user_info={'username':request.user.username, 'email'})

    class Meta:
        path = 'user/login'
        param_fields = (
            ('username', fields.CharField(help_text='用户名')),
            ('password', fields.CharField(help_text='密码')),

class Register(APIBase):

    name = '用户注册'

    def get_context(self, request, *args, **kwargs):
            user = User.objects.create_user(request.params.username,, request.params.password)
        except IntegrityError:
            raise CustomError(ErrCode.ERR_AUTH_USER_EXISTS)
        return self.get_default_context()

    class Meta:
        path = 'user/register'
        param_fields = (
            ('username', fields.RegexField(help_text='用户名', regex=r'^[a-zA-Z0-9_]{3,10}$')),
            ('password', fields.CharField(help_text='密码')),
            ('email', fields.EmailField(help_text='Email')),

class Info(UserAPIBase):

    name = '用户信息'

    def get_context(self, request, *args, **kwargs):
        user_info = {'username':request.user.username, 'email'}

        return self.get_default_context(user_info=user_info)

class Logout(UserAPIBase):

    name = '退出登陆'

    def get_context(self, request, *args, **kwargs):

        return self.get_default_context()

urlpatterns = site.urlpatterns

add to end:

urlpatterns.append(url(r’^test/’, include(“myapp.views”)))


run example:

git clone
cd django_restframework_apiview/example
pip install -r requirements.txt
python makemigrations
python migrate
python runserver
open in browser
api demo::

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-restframework-apiview, version 1.3.12
Filename, size File type Python version Upload date Hashes
Filename, size django_restframework_apiview-1.3.12-py3.6.egg (165.7 kB) File type Egg Python version 3.6 Upload date Hashes View
Filename, size django_restframework_apiview-1.3.12-py3-none-any.whl (76.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django_restframework_apiview-1.3.12.tar.gz (58.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page