【问题标题】:How can I put Variables in URL's using django-qr-code如何使用 django-qr-code 将变量放入 URL 中
【发布时间】:2022-12-21 07:09:44
【问题描述】:

我一直在尝试在我的应用程序的详细信息页面上生成 URL。我希望 QR 码的 url 成为项目详细信息页面的链接。我现在拥有的是:

<img src="{% qr_url_from_text "localhost:8000/items/{{item.id}}" %}" alt="QR-Code for the item">

问题是,我想将 item.id 作为 url 的一部分。但它不被视为变量,所以当我扫描二维码时,它会打开页面http://localhost:8000/items/{{item.id}},但我希望它在 url 中具有实际 ID(例如 4),而不是 {{item.id}}细绳。

有没有办法在这些 URL 名称中放置变量?

已经谢谢你了

【问题讨论】:

    标签: python html django url qr-code


    【解决方案1】:

    不要在模板中使用变量来制作 url。

    在视图中作为上下文变量进行操作,而不是使用该变量代替模板中的“localhost:8000/items/{{item.id}}”。

    所以,在你看来,你会有类似的东西:

    def yourview(request, pk):
        qrcode_url = "localhost:8000/items/" + str(pk)
        context = {
             qrcode_url: 'qrcode_url',
         }
         return render(request, 'yourtemplate.html', context)
    

    而不是在模板中:

    <img src='{% qr_url_from_text qrcode_url size="t" version=10 image_format="png" error_correction="L" %}' alt="QR code">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多