[Django] Auth django app with rest api
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-04-26 [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
2016-04-26 [Angular 2] Mapping Streams to Values to Affect State
2016-04-26 [Angular 2] Managing State in RxJS with StartWith and Scan
2016-04-26 [Angular 2] Handling Clicks and Intervals Together with Merge
2016-04-26 [Angular 2] Handling Click Events with Subjects
2016-04-26 [Angular 2] Rendering an Observable Date with the Async and Date Pipes
2016-04-26 [Angular 2] Rendering an Observable with the Async Pipe