First, start the env:

. bin/activate

Then cd to our module

cd djangular

Create a new app:

python manage.py startapp auth_api

Create a api.py inside auth_api folder:

from django.contrib.auth import authenticate, login, logout
from rest_framework import status, views
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator from .serializers import UserSerializer # views.APIView -- rest api call
class LoginView(views.APIView): @method_decorator(csrf_protect)
def post(self, request):
user = authenticate(
username=request.data.get("username"),
password=request.data.get("password")
) if user is None or not user.is_active:
return Response({
'status': 'Unauthorized',
'message': 'Username or password incorrect'
}, status=status.HTTP_401_UNAUTHORIZED) login(request, user)
return Response(UserSerializer(user).data) # convert python object to json using serializer and send back to client class LogoutView(views.APIView): def get(self, request):
logout(request)
return Response({}, status=status.HTTP_204_NO_CONTENT)

auth_api/serialilzer.py

from django.contrib.auth.models import User

from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):

    class Meta:
model = User
fields = ('id', 'username')

auth_api/urls.py:

from django.conf.urls import url
from .api import LoginView, LogoutView urlpatterns = [
url(r'^login/$', LoginView.as_view()), # because LoginView is class not a method, we need to call as_view() method
url(r'^logout/$', LogoutView.as_view()),
]

top leavel settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'scrurumboard',
'tictactoe',
'auth_api' # add app here
]

top leavel urls.py:

from django.conf.urls import url, include
from django.contrib import admin urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scrumboard/', include('scrurumboard.urls')),
url(r'^auth_api/', include('auth_api.urls')), # add url here
]

If visit the localhost:8000/auth_api/login, should see the interface.

[Django] Auth django app with rest api的更多相关文章

  1. django 添加comments app

    django 添加comments app 参看 django comments 文档 安装和配置comments 1.安装comments,运行:pip install django-contrib ...

  2. django 数据库配置 ,APP 迁移.模型基础

    # 1.数据库的连接配置django 连接mysql的配置流程:- 安装 pymysql pip install pymysql- 创建数据库用户有创建数据库权限的用户- 创建数据库crm .进入数据 ...

  3. Django Auth 专题

    Django的标准库存放在 django.contrib 包中.每个子包都是一个独立的附加功能包. 这些子包一般是互相独立的,不过有些django.contrib子包需要依赖其他子包,其中django ...

  4. Django-中间件-csrf扩展请求伪造拦截中间件-Django Auth模块使用-效仿 django 中间件配置实现功能插拔式效果-09

    目录 昨日补充:将自己写的 login_auth 装饰装在 CBV 上 django 中间件 django 请求生命周期 ***** 默认中间件及其大概方法组成 中间件的执行顺序 自定义中间件探究不同 ...

  5. django框架创建app及使用、

    App 创建一个app : python manage.py startapp app01 admin: from django.contrib import admin # Register you ...

  6. Django中的APP

    3. Django中的APP: 什么是APP?以及为什么要用APP? project --> 项目 (老男孩教育大学校) APP --> 应用 (Linux学院/Python学院/大数据学 ...

  7. Django auth 登陆后页面跳转至/account/profile,修改跳转至其他页面

    这几天在学习django,django功能很强大,自带的auth,基本可以满足用户注册登陆登出,简单的用户注册登陆系统使用django auth足矣.当然也不是拿来就能用的,需要自己写登陆页面的模板, ...

  8. CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境, 记坑篇

    CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境 CentOS7 + Python3 + D ...

  9. django根据不同app配置相应的log文件

    django根据不同app配置相应的log文件 settings.py # django logging LOG_PATH = "/var/log/blog/" LOGGING = ...

随机推荐

  1. 命令行SVN的使用

    1.检出svn  co  http://路径(目录或文件的全路径) [本地目录全路径]  --username 用户名 --password 密码svn  co  svn://路径(目录或文件的全路径 ...

  2. 使IIS服务器支持下载 apk/ipa 安装包

    默认情况下,使用IIS作为Web服务器的无法下载此文件,访问会触发404错误,服务器找不到对应资源. IIS服务器不能下载.apk文件的原因:iis的默认MIME类型中没有.apk文件,所以无法下载. ...

  3. BZOJ1503: [NOI2004]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...

  4. 业余学习react 学习记录

    http://www.ruanyifeng.com/blog/2015/03/react (阮一峰 react 学习) 1.搭建环境:npm 使用 React npm install -g cnpm ...

  5. [Angular] Custom directive Form validator

    Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...

  6. Android睡眠唤醒机制--Kernel态

    一.简介 Android系统中定义了几种低功耗状态:earlysuspend.suspend.hibernation.       1) earlysuspend: 是一种低功耗的状态,某些设备可以选 ...

  7. 【编程】辨异 —— proxy 与 delegate

    二者分别对应着设计模式中的代理模式和委托模式. proxy:译为代理, 被代理方(B)与代理方(A)的接口完全一致. 主要使用场景(语义)应该是:为简化编程(或无法操作B),不直接把请求交给被代理方( ...

  8. How to remove a Data Guard Configuration from Primary Database (文档 ID 733794.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.3 [Release 10.1 to 11. ...

  9. 转载的:Python os 和 os.path模块详解

    os.getcwd()获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curdi ...

  10. Unity自带网络功能——NetworkView组件、Serialize、RPC

    Unity拥有大量的第三方插件,专门提供了对网络功能的支持.可是,大部分开发人员第一次接触到的还是Unity自带的网络功能,也就是大家常常说到的Unity Networking API.这些API是借 ...