+ All Categories
Home > Documents > Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch...

Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch...

Date post: 19-Mar-2020
Category:
Upload: others
View: 14 times
Download: 0 times
Share this document with a friend
47
Modern Websites with Django and React Andrew Neitsch 2016-08-23
Transcript
Page 1: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Modern Websites withDjango and React

Andrew Neitsch

2016-08-23

Page 2: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Introduction

1. Django tutorial

2. JSON APIs with Django REST framework

3. Interactivity with React.js

Conclusion

2 / 22

Page 3: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Disclaimer▶ I don’t know a lot about this

▶ Was starting a new project, looked intowhat’s new in web development

▶ Worked on this presentation instead ofthe project, so just a beginner right now

If you know a better way to do this, pleasegive a talk on it

3 / 22

Page 4: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Disclaimer▶ I don’t know a lot about this▶ Was starting a new project, looked into

what’s new in web development

▶ Worked on this presentation instead ofthe project, so just a beginner right now

If you know a better way to do this, pleasegive a talk on it

3 / 22

Page 5: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Disclaimer▶ I don’t know a lot about this▶ Was starting a new project, looked into

what’s new in web development▶ Worked on this presentation instead of

the project, so just a beginner right now

If you know a better way to do this, pleasegive a talk on it

3 / 22

Page 6: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Disclaimer▶ I don’t know a lot about this▶ Was starting a new project, looked into

what’s new in web development▶ Worked on this presentation instead of

the project, so just a beginner right nowIf you know a better way to do this, pleasegive a talk on it

3 / 22

Page 7: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Introduction

1. Django tutorial

2. JSON APIs with Django REST framework

3. Interactivity with React.js

Conclusion

4 / 22

Page 8: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Django tutorial

https://docs.djangoproject.com/en/1.10/intro/tutorial01/

5 / 22

Page 9: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework
Page 10: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework
Page 11: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework
Page 12: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Getting it running1. Install Python 3

2. git clonehttps://github.com/andrewdotn/django-react.git

3. cd django-react

4. pip3 install --user -r requirements.txt

5. cd polls_tutorial

6. ./manage.py migrate

7. ./manage.py createsuperuser

8. ./manage.py runserver

9 / 22

Page 13: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Introduction

1. Django tutorial

2. JSON APIs with Django REST framework

3. Interactivity with React.js

Conclusion

10 / 22

Page 14: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Requirements for interactivity

Browser needs to retrieve updated state

Browser needs to update state

Standard way of doing this:JSON REST APIs

11 / 22

Page 15: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Requirements for interactivity

Browser needs to retrieve updated state

Browser needs to update state

Standard way of doing this:JSON REST APIs

11 / 22

Page 16: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Requirements for interactivity

Browser needs to retrieve updated state

Browser needs to update state

Standard way of doing this:JSON REST APIs

11 / 22

Page 17: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Requirements for interactivity

Browser needs to retrieve updated state GET

Browser needs to update state POST

Standard way of doing this:JSON REST APIs

11 / 22

Page 20: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework
Page 21: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Commits▶ Add serializer and view to show

questions▶ Wire rest_framework router to URLs▶ Expose full model to API

Hack to workaround namespace issues

▶ Add increment verb$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":26,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":27,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":28,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}

14 / 22

Page 22: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Commits▶ Add serializer and view to show

questions▶ Wire rest_framework router to URLs▶ Expose full model to API▶ Add increment verb

$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":26,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":27,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}$ curl -X POST http://localhost:8000/polls/api/choices/2/increment/; echo{"choice_text":"0","votes":28,"question":"http://localhost:8000/polls/api/questions/2/","url":"http://localhost:8000/polls/api/choices/2/"}

14 / 22

Page 24: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Introduction

1. Django tutorial

2. JSON APIs with Django REST framework

3. Interactivity with React.js

Conclusion

16 / 22

Page 25: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

React.jsThe new standard for JS frontend work

Rules:▶ Components render HTML as a function

of properties and state▶ Properties can only be changed by

parent components▶ Visual updates only happen when

properties or state change in a way thataffects the output of the render function

17 / 22

Page 26: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

React.jsThe new standard for JS frontend work

Rules:

▶ Components render HTML as a functionof properties and state

▶ Properties can only be changed byparent components

▶ Visual updates only happen whenproperties or state change in a way thataffects the output of the render function

17 / 22

Page 27: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

React.jsThe new standard for JS frontend work

Rules:▶ Components render HTML as a function

of properties and state

▶ Properties can only be changed byparent components

▶ Visual updates only happen whenproperties or state change in a way thataffects the output of the render function

17 / 22

Page 28: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

React.jsThe new standard for JS frontend work

Rules:▶ Components render HTML as a function

of properties and state▶ Properties can only be changed by

parent components

▶ Visual updates only happen whenproperties or state change in a way thataffects the output of the render function

17 / 22

Page 29: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

React.jsThe new standard for JS frontend work

Rules:▶ Components render HTML as a function

of properties and state▶ Properties can only be changed by

parent components▶ Visual updates only happen when

properties or state change in a way thataffects the output of the render function

17 / 22

Page 39: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Demo

19 / 22

Page 40: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Introduction

1. Django tutorial

2. JSON APIs with Django REST framework

3. Interactivity with React.js

Conclusion

20 / 22

Page 41: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Conclusions

▶ Exposing an API for your existingmodels is pretty straightforward

▶ React.js is an in-browser template layerthat lets you add live updates withoutchanging rendering code

21 / 22

Page 42: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Conclusions

▶ Exposing an API for your existingmodels is pretty straightforward

▶ React.js is an in-browser template layerthat lets you add live updates withoutchanging rendering code

21 / 22

Page 43: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Future work

▶ Replicate

▶ Pause/faster/slower controls on polling▶ Permissions▶ Pre-compile JSX▶ WebSockets

22 / 22

Page 44: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Future work

▶ Replicate▶ Pause/faster/slower controls on polling

▶ Permissions▶ Pre-compile JSX▶ WebSockets

22 / 22

Page 45: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Future work

▶ Replicate▶ Pause/faster/slower controls on polling▶ Permissions

▶ Pre-compile JSX▶ WebSockets

22 / 22

Page 46: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Future work

▶ Replicate▶ Pause/faster/slower controls on polling▶ Permissions▶ Pre-compile JSX

▶ WebSockets

22 / 22

Page 47: Modern Websites with Django and React - PyYYC...Modern Websites with Django and React Andrew Neitsch 2016-08-23 Introduction 1. Django tutorial 2. JSON APIs with Django REST framework

Future work

▶ Replicate▶ Pause/faster/slower controls on polling▶ Permissions▶ Pre-compile JSX▶ WebSockets

22 / 22


Recommended