django 将用户重定向到URL的操作

0sgqnhkj  于 2023-01-31  发布在  Go
关注(0)|答案(1)|浏览(153)

你能告诉我如何在管理员站点创建一个动作,将用户重定向到某个URL吗?也就是说,在管理员站点,用户选择这个动作,发现自己在谷歌上。

def google(modeladmin, request, queryset):
    """
    I myself write some code here to prevent Django from asking users to select an object.
    """

    URL = "https://google.com"

    ... Here goes the code to redirect the user to URL ...

@admin.register(SemanticsCorePhrases)
class SemanticsCorePhrasesAdmin(admin.ModelAdmin):
    actions = [google, ]
lymnna71

lymnna711#

要将用户重定向到Django管理站点中的URL,可以使用django. shortcuts中的redirect函数。

from django.shortcuts import redirect

def google(modeladmin, request, queryset):

"""I myself write some code here to prevent Django from asking users to select an object.
"""

    URL = "https://google.com"

    return redirect(URL)

@admin.register(SemanticsCorePhrases)
class SemanticsCorePhrasesAdmin(admin.ModelAdmin):
    actions = [google, ]

请参见https://docs.djangoproject.com/en/4.1/topics/http/shortcuts/#:~:text = xhtml%2Bxml%27)-,重定向,-()%C2%B6

相关问题