Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Web Development with Python

You're reading from   Learn Web Development with Python Get hands-on with Python Programming and Django web development

Arrow left icon
Product type Course
Published in Dec 2018
Publisher
ISBN-13 9781789953299
Length 796 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Fabrizio Romano Fabrizio Romano
Author Profile Icon Fabrizio Romano
Fabrizio Romano
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arun Ravindran Arun Ravindran
Author Profile Icon Arun Ravindran
Arun Ravindran
Arrow right icon
View More author details
Toc

Table of Contents (33) Chapters Close

Title Page
About Packt
Contributors
Preface
1. A Gentle Introduction to Python FREE CHAPTER 2. Built-in Data Types 3. Iterating and Making Decisions 4. Functions, the Building Blocks of Code 5. Saving Time and Memory 6. OOP, Decorators, and Iterators 7. Files and Data Persistence 8. Testing, Profiling, and Dealing with Exceptions 9. Concurrent Execution 10. Debugging and Troubleshooting 11. Installing the Required Software and Tools 12. Working with Models, Migrations, Serialization, and Deserialization 13. Creating API Views 14. Using Generalized Behavior from the APIView Class 15. Understanding and Customizing the Browsable API Feature 16. Using Constraints, Filtering, Searching, Ordering, and Pagination 17. Securing the API with Authentication and Permissions 18. Applying Throttling Rules and Versioning Management 19. Automating Tests 20. Solutions 21. Templates 22. Admin Interface 23. Forms 24. Security 25. Working Asynchronously 26. Creating APIs 27. Production-Ready 1. Other Books You May Enjoy Index

Generating and using tokens


Now, we will launch our default Python interactive shell in our virtual environment and make all the Django project modules available to write code that will generate a token for an existing user. We will do this to understand how the token generation works.

Run the following command to launch the interactive shell. Make sure you are within the restful01 folder in the terminal, Command Prompt, or Windows Powershell:

python manage.py shell

You will notice that a line that says (InteractiveConsole) is displayed after the usual lines that introduce your default Python interactive shell. Enter the following code in the Python interactive shell to import all the things we will need to retrieve a User instance and generate a new token. The code file for the sample is included in the hillar_django_restful_08_02 folder, in the restful01/tokens_test_01.py file.

from rest_framework.authtoken.models import Token 
from django.contrib.auth.models import User 

Enter the following...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime