Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django在使用默认视图时将自定义变量传递给模板_Python_Python 3.x_Django_Django Templates_Django Urls - Fatal编程技术网

Python Django在使用默认视图时将自定义变量传递给模板

Python Django在使用默认视图时将自定义变量传递给模板,python,python-3.x,django,django-templates,django-urls,Python,Python 3.x,Django,Django Templates,Django Urls,我目前使用的是默认的django视图(PasswordChangeView),但我有自己的自定义模板要与之配合使用。我通过写下以下内容来实现这一点: path('changepassword/', PasswordChangeView.as_view(template_name='account/change_password.html', success_url = '/changepassword/'), name=

我目前使用的是默认的django视图(PasswordChangeView),但我有自己的自定义模板要与之配合使用。我通过写下以下内容来实现这一点:

path('changepassword/', PasswordChangeView.as_view(template_name='account/change_password.html', 
                                     success_url = '/changepassword/'), name='password_change'),

但是在这个模板中有这样一个变量:
{{{var}}
。有没有办法从URL.py向该变量传递值?或者我需要创建一个全新的自定义视图来传递这个变量吗?

是的,您可以通过以下方式传递额外的参数:

path(
    'changepassword/',
    PasswordChangeView.as_view(
        template_name='account/change_password.html', 
        success_url = '/changepassword/'
    ),
    name='password_change',
    kwargs={'var': value_of_var}
),
路径(
“更改密码/”,
PasswordChangeView.as\u视图(
模板\u name='account/change\u password.html',
成功\u url='/changepassword/'
),
name='password\u change',
kwargs={'var':值{u var}
),

在这里,您用
{{var}}
应该使用的值替换{var的
值。这是因为URL参数也作为变量传递给模板。

很抱歉,我的问题不够具体。这适用于固定值变量,但有没有任何方法可以根据“请求”传递内容?(比如request.user.is_anonymous,通常我可以在视图中使用。)这样:但是对于较新的djangoversion@figbar:请求
对象也会传递给模板,因此,您可以在模板中写入
{%if request.user.is_authenticated%}…{%endif%}