mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2026-03-13 10:32:25 +08:00
update docs
This commit is contained in:
@@ -22,11 +22,11 @@ There are some configs to configure the admin app, and you need to configure it
|
||||
```python
|
||||
from fastapi_admin.app import app as admin_app
|
||||
from fastapi_admin.providers.login import UsernamePasswordProvider
|
||||
from examples.models import User
|
||||
from examples.models import Admin
|
||||
import aioredis
|
||||
from fastapi import FastAPI
|
||||
|
||||
login_provider = UsernamePasswordProvider(user_model=User, enable_captcha=True)
|
||||
login_provider = UsernamePasswordProvider(admin_model=Admin, enable_captcha=True)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@@ -77,12 +77,11 @@ The `Model` make a TortoiseORM model as a menu with CURD page.
|
||||
|
||||
from examples.models import Admin
|
||||
from fastapi_admin.app import app
|
||||
from fastapi_admin.resources import Field, Model, Action
|
||||
from fastapi_admin.widgets import displays, filters, inputs
|
||||
from typing import List
|
||||
from fastapi_admin.file_upload import FileUpload
|
||||
from fastapi_admin.resources import Field, Model
|
||||
from fastapi_admin.widgets import displays, filters, inputs
|
||||
|
||||
upload_provider = FileUpload(uploads_dir=os.path.join(BASE_DIR, "static", "uploads"))
|
||||
upload = FileUpload(uploads=os.path.join(BASE_DIR, "static", "uploads"))
|
||||
|
||||
|
||||
@app.register
|
||||
@@ -112,17 +111,10 @@ class AdminResource(Model):
|
||||
name="avatar",
|
||||
label="Avatar",
|
||||
display=displays.Image(width="40"),
|
||||
input_=inputs.Image(null=True, upload_provider=upload_provider),
|
||||
input_=inputs.Image(null=True, upload=upload),
|
||||
),
|
||||
"created_at",
|
||||
]
|
||||
can_create = False
|
||||
|
||||
async def get_actions(self,request:Request) -> List[Action]:
|
||||
return []
|
||||
|
||||
async def get_bulk_actions(self,request:Request) -> List[Action]:
|
||||
return []
|
||||
```
|
||||
|
||||
### Dropdown
|
||||
|
||||
@@ -1,72 +1,39 @@
|
||||
# Configuration
|
||||
|
||||
The following configurations can be used to `admin_app.configure(...)`.
|
||||
You should configure predefined `app` from `fastapi-admin` on startup event of `fastapi`, because which should be in
|
||||
asyncio loop context.
|
||||
|
||||
## logo_url
|
||||
```python
|
||||
import aioredis
|
||||
from fastapi import FastAPI
|
||||
from fastapi_admin.app import app as admin_app
|
||||
from fastapi_admin.providers.login import UsernamePasswordProvider
|
||||
from examples.models import Admin
|
||||
|
||||
Will show the logo image in admin dashboard.
|
||||
app = FastAPI()
|
||||
|
||||
## login_logo_url
|
||||
|
||||
Will show the logo in login page.
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
redis = await aioredis.create_redis_pool(address='redis://localhost')
|
||||
await admin_app.configure(
|
||||
logo_url="https://preview.tabler.io/static/logo-white.svg",
|
||||
template_folders=["templates"],
|
||||
providers=[
|
||||
UsernamePasswordProvider(
|
||||
login_logo_url="https://preview.tabler.io/static/logo.svg", admin_model=Admin
|
||||
)
|
||||
],
|
||||
redis=redis,
|
||||
)
|
||||
```
|
||||
|
||||
## admin_path
|
||||
## Parameters
|
||||
|
||||
Default is `/admin`, but you can change to other page.
|
||||
|
||||
## maintenance (💗 Pro only)
|
||||
|
||||
If set to `true`, all pages will be redirected to the `/maintenance` page.
|
||||
|
||||
## redis
|
||||
|
||||
Instance of `aioredis`.
|
||||
|
||||
## default_locale
|
||||
|
||||
Current support `zh` and `en`, default is `en`.
|
||||
|
||||
## template_folders
|
||||
|
||||
Template folders registered to jinja2 and also can be used to override builtin templates.
|
||||
|
||||
## login_provider
|
||||
|
||||
You can pass subclasses of `fastapi_admin.providers.login.LoginProvider`, there is a
|
||||
builtin `fastapi_admin.providers.login.UsernamePasswordProvider` you can use.
|
||||
|
||||
### admin_model
|
||||
|
||||
Subclass instance of `fastapi_admin.models.AbstractAdmin`.
|
||||
|
||||
### enable_captcha (💗 Pro only)
|
||||
|
||||
Show captcha in admin login page.
|
||||
|
||||
## permission_provider (💗 Pro only)
|
||||
|
||||
You can enable permission control by setting `permission_provider`, which is instance
|
||||
of `fastapi_admin.providers.permission.PermissionProvider` of its subclass.
|
||||
|
||||
### admin_model
|
||||
|
||||
Subclass of `fastapi_admin.models.AbstractAdmin`.
|
||||
|
||||
### resource_model
|
||||
|
||||
Subclass of `fastapi_admin.models.AbstractResource`.
|
||||
|
||||
### permission_model
|
||||
|
||||
Subclass of `fastapi_admin.models.AbstractPermission`.
|
||||
|
||||
## admin_log_provider
|
||||
|
||||
You can enable action log by set `admin_log_provider`, which is instance
|
||||
of `fastapi_admin.providers.admin_log.AdminLogProvider` or its subclass. After set that, all `delete/create/update` for
|
||||
model will be recorded.
|
||||
|
||||
## search_provider (💗 Pro only)
|
||||
|
||||
You can enable global search by setting `search_provider`, which is instance
|
||||
of `fastapi_admin.providers.search.SearchProvider`, and then will display a search input in all pages.
|
||||
- `logo_url`: Will show the logo image in admin dashboard.
|
||||
- `admin_path`: Default is `/admin`, but you can change to other path.
|
||||
- `maintenance`: If set to `true`, all pages will be redirected to the `/maintenance` page. (💗 Pro only)
|
||||
- `redis`: Instance of `aioredis`.
|
||||
- `default_locale`: Current support `zh` and `en`, default is `en`.
|
||||
- `template_folders`: Template folders registered to jinja2 and also can be used to override builtin templates.
|
||||
- `providers`: List of providers to register, all are subclasses of `fastapi_admin.providers.Provider`.
|
||||
|
||||
@@ -1,43 +1,25 @@
|
||||
# Resource
|
||||
|
||||
There are three kinds of resources, `Link`,`Model` and `Dropdown`.
|
||||
|
||||
## General configuration
|
||||
|
||||
The following are the general configurations of three kinds of resources.
|
||||
|
||||
### label
|
||||
|
||||
Nav bar menu name.
|
||||
|
||||
### icon
|
||||
|
||||
Icon name comes from <https://fontawesome.com>, like `fas fa-user` will display a user icon in menu.
|
||||
There are three kinds of resources, `Link`,`Model` and `Dropdown`, all are inherited
|
||||
from `fastapi_admin.resources.Resource`.
|
||||
|
||||
You should use `app.register` decorator to register a resource.
|
||||
## Link
|
||||
|
||||
General `a` tag menu.
|
||||
`Link` just display a menu with a link.
|
||||
|
||||
### url
|
||||
```python
|
||||
from fastapi_admin.app import app
|
||||
from fastapi_admin.resources import Link
|
||||
|
||||
`href` attr of `a` tag.
|
||||
|
||||
### target
|
||||
|
||||
`target` attr of `a` tag, default is `_self`.
|
||||
|
||||
## Field
|
||||
|
||||
## Action
|
||||
@app.register
|
||||
class Home(Link):
|
||||
label = "Home"
|
||||
icon = "fas fa-home"
|
||||
url = "/admin"
|
||||
```
|
||||
|
||||
## Model
|
||||
|
||||
### get_actions
|
||||
|
||||
### get_bulk_actions
|
||||
|
||||
### row_attributes
|
||||
|
||||
### cell_attributes
|
||||
|
||||
## Dropdown
|
||||
|
||||
@@ -37,9 +37,12 @@ def create_app():
|
||||
)
|
||||
await admin_app.configure(
|
||||
logo_url="https://preview.tabler.io/static/logo-white.svg",
|
||||
login_logo_url="https://preview.tabler.io/static/logo.svg",
|
||||
template_folders=[os.path.join(BASE_DIR, "templates")],
|
||||
providers=[LoginProvider(admin_model=Admin)],
|
||||
providers=[
|
||||
LoginProvider(
|
||||
login_logo_url="https://preview.tabler.io/static/logo.svg", admin_model=Admin
|
||||
)
|
||||
],
|
||||
redis=redis,
|
||||
)
|
||||
|
||||
|
||||
@@ -33,14 +33,12 @@ class FastAPIAdmin(FastAPI):
|
||||
self,
|
||||
redis: Redis,
|
||||
logo_url: str = None,
|
||||
login_logo_url: str = None,
|
||||
default_locale: str = "en_US",
|
||||
admin_path: str = "/admin",
|
||||
template_folders: Optional[List[str]] = None,
|
||||
providers: Optional[List[Provider]] = None,
|
||||
):
|
||||
self.redis = redis
|
||||
self.login_logo_url = login_logo_url
|
||||
i18n.set_locale(default_locale)
|
||||
self.admin_path = admin_path
|
||||
self.logo_url = logo_url
|
||||
|
||||
@@ -34,18 +34,27 @@ class UsernamePasswordProvider(Provider):
|
||||
logout_path="/logout",
|
||||
template="login.html",
|
||||
login_title="Login to your account",
|
||||
login_logo_url: str = None,
|
||||
):
|
||||
self.login_path = login_path
|
||||
self.logout_path = logout_path
|
||||
self.template = template
|
||||
self.admin_model = admin_model
|
||||
self.login_title = login_title
|
||||
self.login_logo_url = login_logo_url
|
||||
|
||||
async def login_view(
|
||||
self,
|
||||
request: Request,
|
||||
):
|
||||
return templates.TemplateResponse(self.template, context={"request": request})
|
||||
return templates.TemplateResponse(
|
||||
self.template,
|
||||
context={
|
||||
"request": request,
|
||||
"login_logo_url": self.login_logo_url,
|
||||
"login_title": self.login_title,
|
||||
},
|
||||
)
|
||||
|
||||
async def register(self, app: "FastAPIAdmin"):
|
||||
await super(UsernamePasswordProvider, self).register(app)
|
||||
|
||||
@@ -1,93 +1,93 @@
|
||||
{% extends "base.html" %} {% block outer_body %}
|
||||
<body class="border-primary d-flex flex-column">
|
||||
<div class="page page-center">
|
||||
<div class="container-tight py-4">
|
||||
<div class="text-center mb-4">
|
||||
<a href="."
|
||||
><img
|
||||
src="https://preview.tabler.io/static/logo.svg"
|
||||
height="36"
|
||||
alt=""
|
||||
/></a>
|
||||
</div>
|
||||
{% if error %}
|
||||
<div
|
||||
class="alert alert-important alert-danger alert-dismissible"
|
||||
role="alert"
|
||||
>
|
||||
<div class="d-flex">
|
||||
<div>
|
||||
<i class="ti ti-alert-circle"></i>
|
||||
</div>
|
||||
<div>{{ error }}</div>
|
||||
</div>
|
||||
<a
|
||||
class="btn-close btn-close-white"
|
||||
data-bs-dismiss="alert"
|
||||
aria-label="close"
|
||||
></a>
|
||||
</div>
|
||||
{% endif %}
|
||||
<form
|
||||
class="card card-md"
|
||||
action="{{ request.app.admin_path }}{{ request.app.login_provider.login_path }}"
|
||||
method="post"
|
||||
autocomplete="off"
|
||||
>
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-center mb-4">
|
||||
{{ request.app.login_provider.login_title }}
|
||||
</h2>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">{{ _('username') }}</label>
|
||||
<input
|
||||
name="username"
|
||||
type="text"
|
||||
class="form-control"
|
||||
placeholder="{{ _('login_username_placeholder') }}"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label">
|
||||
{{ _('password') }}
|
||||
<span class="form-label-description"> </span>
|
||||
</label>
|
||||
<div class="input-group input-group-flat">
|
||||
<input
|
||||
placeholder="{{ _('login_password_placeholder') }}"
|
||||
name="password"
|
||||
type="password"
|
||||
class="form-control"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<span class="input-group-text"> </span>
|
||||
<body class="border-primary d-flex flex-column">
|
||||
<div class="page page-center">
|
||||
<div class="container-tight py-4">
|
||||
<div class="text-center mb-4">
|
||||
<a href="."
|
||||
><img
|
||||
src="{{ login_logo_url }}"
|
||||
height="36"
|
||||
alt=""
|
||||
/></a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="remember_me"
|
||||
class="form-check-input"
|
||||
/>
|
||||
<span class="form-check-label">{{ _('remember_me') }}</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-footer">
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
{{ _('sign_in') }}
|
||||
</button>
|
||||
</div>
|
||||
{% if error %}
|
||||
<div
|
||||
class="alert alert-important alert-danger alert-dismissible"
|
||||
role="alert"
|
||||
>
|
||||
<div class="d-flex">
|
||||
<div>
|
||||
<i class="ti ti-alert-circle"></i>
|
||||
</div>
|
||||
<div>{{ error }}</div>
|
||||
</div>
|
||||
<a
|
||||
class="btn-close btn-close-white"
|
||||
data-bs-dismiss="alert"
|
||||
aria-label="close"
|
||||
></a>
|
||||
</div>
|
||||
{% endif %}
|
||||
<form
|
||||
class="card card-md"
|
||||
action="{{ request.app.admin_path }}{{ request.app.login_provider.login_path }}"
|
||||
method="post"
|
||||
autocomplete="off"
|
||||
>
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-center mb-4">
|
||||
{{ login_title }}
|
||||
</h2>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">{{ _('username') }}</label>
|
||||
<input
|
||||
name="username"
|
||||
type="text"
|
||||
class="form-control"
|
||||
placeholder="{{ _('login_username_placeholder') }}"
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label">
|
||||
{{ _('password') }}
|
||||
<span class="form-label-description"> </span>
|
||||
</label>
|
||||
<div class="input-group input-group-flat">
|
||||
<input
|
||||
placeholder="{{ _('login_password_placeholder') }}"
|
||||
name="password"
|
||||
type="password"
|
||||
class="form-control"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<span class="input-group-text"> </span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="remember_me"
|
||||
class="form-check-input"
|
||||
/>
|
||||
<span class="form-check-label">{{ _('remember_me') }}</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-footer">
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
{{ _('sign_in') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
|
||||
<script>
|
||||
let on = Cookies.get("remember_me");
|
||||
if (on !== "") {
|
||||
$('input[name="remember_me"]').attr("checked", true);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
|
||||
<script>
|
||||
let on = Cookies.get("remember_me");
|
||||
if (on !== "") {
|
||||
$('input[name="remember_me"]').attr("checked", true);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user