【问题标题】:Djnago Template - Url with multiple parameterDjango 模板 - 具有多个参数的 URL
【发布时间】:2021-01-29 04:56:30
【问题描述】:

我正在使用 Django 模板。我想在 URL 中添加多个参数

目前我只传递一个参数

我的 reset_password.html

Click on this link to reset your password 
{% if htmlVersion %}
    <div>
        <a href="{{domain}}{% url 'pweuser:user_password_sms_reset' token  %}">
            {{domain}}{% url 'pweuser:user_password_sms_reset' token %} 
        </a>
    </div>
{% else %}
{{domain}}{% url 'pweuser:user_password_sms_reset' token %}
{% endif %}
This link will expire in 15 minutes

我的 urls.py

url(r"^resetPasswordSms/(?P<token>[-\w_=]{28})/$", PasswordResetSmsView.as_view(), name="user_password_sms_reset",),

我的意见.py

    t = loader.get_template("reset_password.html")
    c = {"htmlVersion": True, "domain": settings.SITE_DOMAIN, "token": token.token}
    htmlVersion = t.render(c)
    c = {"htmlVersion": False, "domain": settings.SITE_DOMAIN, "token": token.token}
    textVersion = t.render(c)

这里运行良好

在此我想添加超过 1 个参数。表示这里的 token 是第一个参数,我需要添加 userId 作为第二个参数并且需要传入模板..

如何在 URL 和模板 URL 中添加多个参数

【问题讨论】:

    标签: python-3.x django django-views django-templates


    【解决方案1】:

    在 URL 中添加多个参数的方法如下:

    from django.urls import re_path
    urlpatterns = [
    re_path(r"^resetPasswordSms/(?P<userId>[0-9])/(?P<token>[-\w_=]{28})/$", PasswordResetSmsView.as_view(), name="user_password_sms_reset")
    ]
    

    https://docs.djangoproject.com/en/3.1/topics/http/urls/#using-regular-expressions

    和模板网址:

    {% url 'pweuser:user_password_sms_reset' token=value1 userId=value2   %}
    

    https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url

    如果您对任何部分有任何疑问,可以参考下面的链接。

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 2014-04-21
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      相关资源
      最近更新 更多