在基于Django类的vi中加载基于URL的不同HTML页面

2024-06-02 15:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我的网址.py是否有此代码:

login_view = LoginView.as_view()
url(r'^login$', login_view, name='login'),

我已经在我的视图.py公司名称:

^{pr2}$

现在我的要求是,我想为来自不同url的不同用户创建一个登录页面。例如myproject.com/customers。 我可以用一个新的视图类来完成。但是我想让它通用,并在同一个LoginView()类中处理它。在

是否可以捕获url参数并在LoginView()中检查并加载其他登录.html为他们传呼?或者有没有其他方法可以用来处理这种情况?在


Tags: 代码用户namepy名称view视图url
1条回答
网友
1楼 · 发布于 2024-06-02 15:38:32

由于您使用的是FormView,因此可以像下面的示例一样重写以下方法。模板在render_to_response方法中呈现。在

def render_to_response(self, context, **response_kwargs):
    """
    Returns a response, using the `response_class` for this
    view, with a template rendered with the given context.
    If any keyword arguments are provided, they will be
    passed to the constructor of the response class.
    """

    # Write you logic here
    if self.request.something == "customer":
        template = "customer_login.html"
    else:
        template = "user_login.html"
    #######
    response_kwargs.setdefault('content_type', self.content_type)
    return self.response_class(
        request=self.request,
        template=template,
        context=context,
        using=self.template_engine,
        **response_kwargs
    )

您可以https://ccbv.co.uk/projects/Django/1.11/django.views.generic.edit/FormView/

相关问题 更多 >