DRF获取DefaultRouter对应的url
命令
python manage.py show_urls
urls.py
from user.router import core_routerurlpatterns = [
path("user/login/", views.LoginView.as_view(), name="login"),
path("user/logout/", views.LogoutView.as_view(), name="logout"),
path("user/register/", views.UserRegisterView.as_view(), name="register"),
path("user/forget/", views.UserForgetView.as_view(), name="forget"),
# path("code/image/", views.LoginView.as_view()),
]
urlpatterns += core_router.urls
router.py
from rest_framework.routers import DefaultRouterfrom user import views
core_router = DefaultRouter()
core_router.register("user", views.UserViewSet, base_name="user")
core_router.register("code_sms", views.SmsCodeViewSet, base_name="code_sms")
test.py使用反向url
class UserAPIViewsTestCase(APITestCase): username = "test"
phone = "xxxx"
password = "test1234"
def setUp(self):
User.objects.create_user(
username=self.username,
phone=self.phone,
password=self.password,
)
self.login_url = reverse("login")
self.logout_url = reverse("logout")
self.forget_url = reverse("forget")
self.register_url = reverse("register")
self.code_sms_url = reverse("code_sms")
def test_login(self):
resp = self.client.post(self.login_url, {
"username": self.phone,
"password": self.password + "123",
# "password": self.password,
})
查看所有的url
() ➜ git:(master) ✗ python manage.py show_urls
/__debug__/render_panel/ debug_toolbar.views.render_panel djdt:render_panel
/__debug__/sql_explain/ debug_toolbar.panels.sql.views.sql_explain djdt:sql_explain
/__debug__/sql_profile/ debug_toolbar.panels.sql.views.sql_profile djdt:sql_profile
/__debug__/sql_select/ debug_toolbar.panels.sql.views.sql_select djdt:sql_select
/__debug__/template_source/ debug_toolbar.panels.templates.views.template_source djdt:template_source
/admin/ django.contrib.admin.sites.index admin:index
/admin/<app_label>/ django.contrib.admin.sites.app_index admin:app_list
/admin/auth/group/ django.contrib.admin.options.changelist_view admin:auth_group_changelist
/admin/auth/group/<path:object_id>/ django.views.generic.base.RedirectView
/admin/auth/group/<path:object_id>/change/ django.contrib.admin.options.change_view admin:auth_group_change
/admin/auth/group/<path:object_id>/delete/ django.contrib.admin.options.delete_view admin:auth_group_delete
/admin/auth/group/<path:object_id>/history/ django.contrib.admin.options.history_view admin:auth_group_history
/admin/auth/group/add/ django.contrib.admin.options.add_view admin:auth_group_add
/admin/auth/group/autocomplete/ django.contrib.admin.options.autocomplete_view admin:auth_group_autocomplete
/admin/django_celery_results/taskresult/ django.contrib.admin.options.changelist_view admin:django_celery_results_taskresult_changelist
/admin/django_celery_results/taskresult/<path:object_id>/ django.views.generic.base.RedirectView
/admin/django_celery_results/taskresult/<path:object_id>/change/ django.contrib.admin.options.change_view admin:django_celery_results_taskresult_change
/admin/django_celery_results/taskresult/<path:object_id>/delete/ django.contrib.admin.options.delete_view admin:django_celery_results_taskresult_delete
/admin/django_celery_results/taskresult/<path:object_id>/history/ django.contrib.admin.options.history_view admin:django_celery_results_taskresult_history
/admin/django_celery_results/taskresult/add/ django.contrib.admin.options.add_view admin:django_celery_results_taskresult_add
/admin/django_celery_results/taskresult/autocomplete/ django.contrib.admin.options.autocomplete_view admin:django_celery_results_taskresult_autocomplete
/admin/jsi18n/ django.contrib.admin.sites.i18n_javascript admin:jsi18n
/admin/login/ django.contrib.admin.sites.login admin:login
/admin/logout/ django.contrib.admin.sites.logout admin:logout
/admin/password_change/ django.contrib.admin.sites.password_change admin:password_change
/admin/password_change/done/ django.contrib.admin.sites.password_change_done admin:password_change_done
/admin/r/<int:content_type_id>/<path:object_id>/ django.contrib.contenttypes.views.shortcut admin:view_on_site
/admin/user/user/ django.contrib.admin.options.changelist_view admin:user_user_changelist
/admin/user/user/<id>/password/ django.contrib.auth.admin.user_change_password admin:auth_user_password_change
/admin/user/user/<path:object_id>/ django.views.generic.base.RedirectView
/admin/user/user/<path:object_id>/change/ django.contrib.admin.options.change_view admin:user_user_change
/admin/user/user/<path:object_id>/delete/ django.contrib.admin.options.delete_view admin:user_user_delete
/admin/user/user/<path:object_id>/history/ django.contrib.admin.options.history_view admin:user_user_history
/admin/user/user/add/ django.contrib.auth.admin.add_view admin:user_user_add
/admin/user/user/autocomplete/ django.contrib.admin.options.autocomplete_view admin:user_user_autocomplete
/admin/user/verifycode/ django.contrib.admin.options.changelist_view admin:user_verifycode_changelist
/admin/user/verifycode/<path:object_id>/ django.views.generic.base.RedirectView
/admin/user/verifycode/<path:object_id>/change/ django.contrib.admin.options.change_view admin:user_verifycode_change
/admin/user/verifycode/<path:object_id>/delete/ django.contrib.admin.options.delete_view admin:user_verifycode_delete
/admin/user/verifycode/<path:object_id>/history/ django.contrib.admin.options.history_view admin:user_verifycode_history
/admin/user/verifycode/add/ django.contrib.admin.options.add_view admin:user_verifycode_add
/admin/user/verifycode/autocomplete/ django.contrib.admin.options.autocomplete_view admin:user_verifycode_autocomplete
/api-auth/login/ django.contrib.auth.views.LoginView rest_framework:login
/api-auth/logout/ django.contrib.auth.views.LogoutView rest_framework:logout
/api/ rest_framework.routers.APIRootView api-root
/api/.<format>/ rest_framework.routers.APIRootView api-root
/api/code_sms/ user.views.SmsCodeViewSet code_sms-list
/api/code_sms.<format>/ user.views.SmsCodeViewSet code_sms-list
/api/user/ user.views.UserViewSet user-list
/api/user/<pk>/ user.views.UserViewSet user-detail
/api/user/<pk>.<format>/ user.views.UserViewSet user-detail
/api/user/forget/ user.views.UserForgetView forget
/api/user/login/ user.views.LoginView login
/api/user/logout/ user.views.LogoutView logout
/api/user/register/ user.views.UserRegisterView register
/api/user.<format>/ user.views.UserViewSet user-list
/docs/ drf_yasg.views.SchemaView schema-swagger-ui
/media/<path> django.views.static.serve
/redoc/ drf_yasg.views.SchemaView schema-redoc
/swagger<format> drf_yasg.views.SchemaView schema-json
() ➜ git:(master) ✗
以上是 DRF获取DefaultRouter对应的url 的全部内容, 来源链接: utcz.com/z/511013.html