-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added astro django menu entry for article * Correct Rails to Flask typo * Change references to new quickstart doc everywhere * Work on python doc * Getting old django quickstart to work * Old django quickstart now works complete in astro with code. Now to make it meet the new standard * English * English * Finished draft * text updates * text updates * Python Django Quickstart language edit * Python Django Quickstart proofread * Update astro/src/content/quickstarts/quickstart-python-django-web.mdx Co-authored-by: Dan Moore <dan@fusionauth.io> * Change we to you * remove the django guide example from the example apps data file * Add rediretion for new guide * Remove filename above code snippets * Remove ; from shell commands * Change absolute links to relative * Name file * Remove integrate-python-django.adoc * Change from title case to start case * Tell Dan we don't need to cd at any point * Explain why we are using venv * Remove unnecessary command to start Django server * Clarified all commands in directories * Comments on windows * Add warning about passwords in git * fix relative urls * Revised Python Django Quickstart language edit * Apply suggestions from code review Co-authored-by: Dan Moore <dan@fusionauth.io> * update Elastic to Elasticsearch --------- Co-authored-by: RichardJECooke <RichardJECooke@pm.me> Co-authored-by: Tatenda <31291528+rideam@users.noreply.github.com> Co-authored-by: worktheclock <85885287+worktheclock@users.noreply.github.com> Co-authored-by: Dan Moore <dan@fusionauth.io>
- Loading branch information
1 parent
07b4ae4
commit 82b652e
Showing
11 changed files
with
264 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
astro/src/content/quickstarts/quickstart-python-django-web.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
--- | ||
title: Python Django | ||
description: Quickstart integration of Python Django web application with FusionAuth | ||
navcategory: getting-started | ||
prerequisites: Python 3.8 and pip3 | ||
section: web | ||
technology: Django | ||
language: Python | ||
icon: /img/icons/python.svg | ||
faIcon: fa-snake | ||
color: yellow | ||
cta: EmailListCTA | ||
--- | ||
import Aside from '/src/components/Aside.astro'; | ||
import LoginAfter from '../../diagrams/quickstarts/login-after.astro'; | ||
import LoginBefore from '../../diagrams/quickstarts/login-before.astro'; | ||
import RemoteCode from '/src/components/RemoteCode.astro'; | ||
|
||
|
||
In this quickstart, you will build an application with Python and Django and integrate it with FusionAuth. The application is for [ChangeBank](https://www.youtube.com/watch?v=pkH-kD73QUM), a global leader in converting dollars into coins. It will have areas reserved for logged in users and public-facing sections. | ||
|
||
Find the Docker Compose file and source code for the complete application at https://github.com/FusionAuth/fusionauth-quickstart-python-django-web. | ||
|
||
## Prerequisites | ||
|
||
For this Quickstart, you'll need: | ||
* [Python3.8](https://www.python.org/downloads/) or later. | ||
* [Docker](https://www.docker.com), which is the quickest way to start FusionAuth. (There are [other ways](/docs/v1/tech/installation-guide/)). | ||
|
||
## General Architecture | ||
|
||
While this sample application doesn't have login functionality without FusionAuth, a more typical integration will replace an existing login system with FusionAuth. | ||
|
||
In that case, the system might look like this before FusionAuth is introduced. | ||
|
||
<LoginBefore alt={"Request flow during login before FusionAuth"}/> | ||
|
||
The login flow will look like this after FusionAuth is introduced. | ||
|
||
<LoginAfter alt={"Request flow during login after FusionAuth"}/> | ||
|
||
In general, you would introduce FusionAuth to normalize and consolidate user data, making it consistent and up-to-date and offloading your login security and functionality to FusionAuth. | ||
|
||
## Getting Started | ||
|
||
Start with getting FusionAuth up and running and creating a new Django application. | ||
|
||
### Clone The Code | ||
|
||
First, grab the code from the repository and change into that directory. | ||
|
||
``` | ||
git clone https://github.com/FusionAuth/fusionauth-quickstart-python-django-web.git | ||
cd fusionauth-quickstart-python-django-web | ||
``` | ||
|
||
All shell commands in this guide can be entered in a terminal in this folder. On Windows, you need to replace forward slashes with backslashes in paths. | ||
|
||
The files you'll create in this guide already exist in the `complete-application` folder, if you prefer to copy them. | ||
|
||
### Run FusionAuth Via Docker | ||
|
||
You'll find a Docker Compose file `docker-compose.yml` and an environment variables configuration file `.env` in the root directory of the repo. | ||
|
||
Assuming you have Docker installed, you can start FusionAuth on your machine with the following. | ||
|
||
``` | ||
docker-compose up -d | ||
``` | ||
|
||
This will start three containers, one each for FusionAuth, Postgres, and Elasticsearch. | ||
|
||
Here you are using a bootstrapping feature of FusionAuth, called [Kickstart](/docs/v1/tech/installation-guide/kickstart). When FusionAuth starts for the first time, it will look at the `kickstart/kickstart.json` file and configure FusionAuth to your specified state. | ||
|
||
<Aside type="note"> | ||
If you ever want to reset the FusionAuth system, delete the volumes created by `docker-compose` by executing `docker-compose down -v`, then rerun `docker-compose up -d`. | ||
</Aside> | ||
|
||
FusionAuth will be configured with these settings: | ||
* Your client Id is `e9fdb985-9173-4e01-9d73-ac2d60d1dc8e`. | ||
* Your client secret is `super-secret-secret-that-should-be-regenerated-for-production`. | ||
* Your example username is `richard@example.com` and the password is `password`. | ||
* Your admin username is `admin@example.com` and the password is `password`. | ||
* The base URL of FusionAuth is `http://localhost:9011/`. | ||
|
||
You can log in to the [FusionAuth admin UI](http://localhost:9011/admin) and look around if you want to, but with Docker and Kickstart, everything will already be configured correctly. | ||
|
||
<Aside type="caution"> | ||
The `.env` and `kickstart.json` files contain passwords. In a real application, always add these files to your `.gitignore` file and never commit secrets to version control. | ||
</Aside> | ||
|
||
### Create A Basic Django Application | ||
|
||
Next, you'll set up a basic Django project with a single app. While this guide builds a new Django project, you can use the same method to integrate your existing project with FusionAuth. | ||
|
||
#### Set Up Your Environment | ||
|
||
You should work in a virtual environment for this. | ||
|
||
```shell | ||
python3 -m venv venv && source venv/bin/activate | ||
``` | ||
|
||
Using `venv` isolates Python dependencies for this project locally so that any dependencies used by other projects on your machine are not affected. | ||
|
||
Create a `requirements.txt` file to list the project dependencies with the following content. | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/requirements.txt" | ||
lang="shell" /> | ||
|
||
Then install the dependencies into your virtual environment. | ||
|
||
```shell | ||
pip install -r requirements.txt | ||
``` | ||
|
||
#### Create The Application | ||
|
||
Create the default Django starter project. | ||
|
||
```shell | ||
django-admin startproject mysite | ||
``` | ||
|
||
Now create a `.env` file in your `mysite` folder and add the following to it (note that this is a different environment file to the one in the root folder used by Docker for FusionAuth). | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/mysite/.env" | ||
lang="shell" /> | ||
|
||
## Authentication | ||
|
||
Create an app within your `mysite` project. | ||
|
||
```shell | ||
cd mysite | ||
python manage.py startapp app | ||
cd .. | ||
``` | ||
|
||
Now you will work from the top down to set up authentication URLs in the project `settings.py`, then in the application `urls.py`, then the `views.py`, and finally, the HTML templates for the views. | ||
|
||
### Project Settings | ||
|
||
Overwrite your `mysite/mysite/settings.py` contents with the following code. | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/mysite/mysite/settings.py" lang="python" /> | ||
|
||
Here, your `app` and `mozilla_django_oidc` are added to the list of `INSTALLED_APPS`. The authentication library from Mozilla is set up by `mozilla_django_oidc` and the `OIDC_OP_` constants imported from the `.env` file. This automatically handles the interaction with FusionAuth on the endpoints configured in this file and the matching ones configured in the `kickstart.json` file that you used to initialize FusionAuth. | ||
|
||
### Project URLs | ||
|
||
Overwrite the `mysite/mysite/urls.py` file with the following code. | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/mysite/mysite/urls.py" | ||
lang="python" /> | ||
|
||
* The `admin` site is the default Django admin. | ||
* The `app` URLs are for the app you created in your project. | ||
* The `oidc` URLs are automatically generated from the settings you configured in the previous step. | ||
|
||
### App URLs | ||
|
||
From this point, you'll create files in the `mysite/app` application folder, not in the main `mysite/mysite` project. | ||
|
||
Create a `mysite/app/urls.py` file and set its content to match the project URLs. | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/mysite/app/urls.py" | ||
lang="python" /> | ||
|
||
### App Views | ||
|
||
Now overwrite the `mysite/app/views.py` file. The code below will create three pages: the home page, the account page, and the make change page. | ||
|
||
<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-python-django-web/main/complete-application/mysite/app/views.py" | ||
lang="python" /> | ||
|
||
Here's what these views do: | ||
|
||
* The `app` view, or home page, redirects the user to the `account` view if they are already logged in. | ||
* The `account` view checks whether the user is logged in. If the user is logged in, it returns the account page template, passing in the user's email address returned from FusionAuth. If the user isn't logged in, they are redirected to the login page. This also happens in the change page. | ||
* The `logout` view is necessary because you need to clear the session for the user in Django after the user logs out of FusionAuth. FusionAuth sends the user to this view after logging them out, which then returns them to the home page. | ||
* The `change` view looks longer only because it demonstrates how to do some calculations if the request is a POST instead of a GET. These calculations are then passed to the template in the `change` variable to display onscreen. | ||
|
||
## Customization | ||
|
||
The final step is to create the HTML pages and style them with CSS. Copy the `static` and `templates` folders into your app. Use the appropriate command for your operating system. | ||
|
||
```batch | ||
# linux, mac | ||
cp -r complete-application/mysite/app/static mysite/app | ||
cp -r complete-application/mysite/app/templates mysite/app | ||
# windows | ||
xcopy /e complete-application\mysite\app\static mysite\app | ||
xcopy /e complete-application\mysite\app\templates mysite\app | ||
``` | ||
|
||
There are only two lines in these templates related to authentication. In the `home.html` template, there is a login button. | ||
|
||
```html | ||
<a class="button-lg" href="{% url 'oidc_authentication_init' %}">Login</a> | ||
``` | ||
This URL is made available by the Mozilla plugin in the settings file. It directs the user to FusionAuth to log in, which then redirects back to your app account page. | ||
|
||
In the account and change pages, there is a logout form. | ||
|
||
```html | ||
<form id="logoutForm" class="button-lg" action="{% url 'oidc_logout' %}" method="post"> | ||
{% csrf_token %} | ||
<a class="button-lg" href="#" onclick="document.getElementById('logoutForm').submit();">Log out</a> | ||
</form> | ||
``` | ||
|
||
Log out has to be a form that submits a `POST` request to FusionAuth. Here you use an anchor element that is easy to style, but if your users might disable JavaScript, you could use a traditional form `input` element. | ||
|
||
## Run The Application | ||
|
||
Update the default SQLite database and start your server. | ||
|
||
```bash | ||
python mysite/manage.py migrate | ||
python mysite/manage.py runserver | ||
``` | ||
|
||
You can now browse to [the Django app](http://localhost:8000/app). | ||
|
||
Log in using `richard@example.com` and `password`. The change page will allow you to enter a number and see the result of the POST. Log out and verify that you can't browse to the [account page](http://localhost:8000/app/account). | ||
|
||
## Next Steps | ||
|
||
This quickstart is a great way to get a proof of concept up and running quickly, but to run your application in production, there are some things you're going to want to do. | ||
|
||
### FusionAuth Customization | ||
|
||
FusionAuth gives you the ability to customize just about everything to do with the user's experience and the integration of your application. This includes: | ||
* [Hosted pages](/docs/v1/tech/themes/) such as login, registration, email verification, and many more. | ||
* [Email templates](/docs/v1/tech/email-templates/email-templates). | ||
* [User data and custom claims in access token JWTs](/articles/tokens/jwt-components-explained). | ||
|
||
### Security | ||
|
||
* Learn more about configuring the [open source OIDC library](https://github.com/mozilla/mozilla-django-oidc). | ||
* You may want to customize the [token expiration times and policies](/docs/v1/tech/oauth/#configure-application-oauth-settings) in FusionAuth. | ||
* Choose [password rules](/docs/v1/tech/core-concepts/tenants#password) and a [hashing algorithm](/docs/v1/tech/reference/password-hashes) that meet your security needs. | ||
|
||
### Tenant And Application Management | ||
|
||
* Model your application topology using [Applications](/docs/v1/tech/core-concepts/applications), [Roles](/docs/v1/tech/core-concepts/roles), [Groups](/docs/v1/tech/core-concepts/groups), [Entities](/docs/v1/tech/core-concepts/groups), and more. | ||
* Set up [MFA](/docs/v1/tech/guides/multi-factor-authentication), [Social login](/docs/v1/tech/identity-providers/), or [SAML](/docs/v1/tech/identity-providers/samlv2/) integrations. | ||
* Integrate with external systems using [Webhooks](/docs/v1/tech/events-webhooks/), [SCIM](/docs/v1/tech/core-concepts/scim), and [Lambdas](/docs/v1/tech/lambdas/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
site/docs/v1/tech/shared/_framework-integration-tutorial-list.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
* link:/docs/v1/tech/tutorials/integrate-java-spring[Java Spring Quickstart] | ||
* link:/docs/v1/tech/tutorials/integrate-expressjs[Express Quickstart] | ||
* link:/docs/v1/tech/tutorials/integrate-python-django[Python Django Quickstart] | ||
* link:/docs/quickstarts/quickstart-python-django-web[Python Django Quickstart] | ||
* link:/docs/v1/tech/tutorials/integrate-python-flask[Python Flask Quickstart] | ||
* link:/docs/v1/tech/tutorials/integrate-react[React Quickstart] | ||
* link:/docs/v1/tech/tutorials/integrate-ruby-rails[All Quickstarts] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.