Using django model/authentication/authorization within Tornado
5407 단어 Authentication
But ususually we want to get good use of it. And we don't want to build another system to manage 'user' information.
So, we can use django within tornado. I mean use tornado more.
We can build an service application using tornado.
For example, there is a file for tornado which would look like this:
It's a hello world tornado app. save it as tornado_service.py
___________________
Hello, world
Here is a simple “Hello, world” example web app for Tornado:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
This example does not use any of Tornado’s asynchronous features; for that see this simple chat room .
___________________
Make django work within tornado
import tornado.ioloop
import tornado.web
import os
# the settings refers to a file settings.py (it's the django main project's settings.py )
# just add the route in the os.environ[]
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# now you can use the authenticate lib from django
# the settings could include many setting details such as 'database', 'path', and etc..
from django.contrib.auth import authenticate
class MainHandler(tornado.web.RequestHandler):
def get(self):
username = self.get_argument("username")
password = self.get_argument("password")
user = authenticate(username= username, password = password)
if user:
self.write("hello " + user.username)
else:
self.write("Wrong username or password! :(" )
return
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
then run this tornado app:
$ python tornado_service.py
In your web browser, go to http://127.0.0.1:8888/?username=jake&password=anderson
If the password matches, then you will get
hello jake
If doesn't, then
Wrong username or password! :(
Now you can make django work within torando.
Have fun! Happy hacking!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
React(Next.js) × Firebase에서 전체 개발 인증 주위로그인 로그인하지 않았을 때 특정 페이지에 들어가려면 경보 & 이동 로그인 시 로그인 페이지 등에 가면 경보 & 이전 실제 로그인 처리는 사용자 로그인과 같은 줄만 있습니다. await auth.signInWithE...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.