在 Jinja 模板中使用条件逻辑处理用户选择(凯撒密码 Django 网络应用程序)

Handling user choice with conditional logic in Jinja template (caesar cipher Django web app)

我正在编写一个 Django 网络应用程序,它基本上为网络访问者提供了使用非常基本的凯撒密码加密/解密消息的选项。我的加密功能可以正常工作,但是当我尝试在我的模板中实现条件逻辑来处理用户在登录页面上的初始选择(加密或解密的选项)时,Django 没有为客户端提供该选项。我不知道为什么。

我期待 Django 在登陆页面上给网络访问者这个选择:

Would you like to encrypt a plaintext message? Or would you like to decrypt a scrambled message?

(...其中每个问题都是一个超链接,可将访​​问者带到一个页面以输入他们的消息)。

当网络访问者登陆页面时,两个选项都没有显示。 Django 只提供一个空白模板。

这是我的开发服务器的临时镜像:https://d0d342ea4934.ngrok.io

这是我的 views.py:

from django.shortcuts import render
import string

def caesar(request):
    # Courtesy of Real Python: https://realpython.com/python-practice-problems/
    if 'encrypt' in request.GET:
        plain_text = request.GET['original_entry']
        shift_num = request.GET['shift_num']
        shift_num = int(shift_num)
        letters = string.ascii_lowercase
        mask = letters[shift_num:] + letters[:shift_num]
        trantab = str.maketrans(letters, mask)
        output_text = plain_text.translate(trantab)
        context = {'select_option': False, 'output_text': output_text, 'original_entry': plain_text, 'conv_decrypt': False, 'conv_encrypt': True}
        return render(request, 'landings/home.html', context)
    
    elif 'decrypt' in request.GET:
        plain_text = request.GET['original_entry']
        shift_num = request.GET['shift_num']
        shift_num = -int(shift_num)
        letters = string.ascii_lowercase
        mask = letters[shift_num:] + letters[:shift_num]
        trantab = str.maketrans(letters, mask)
        output_text = plain_text.translate(trantab)
        context = {'select_option': False, 'output_text': output_text, 'original_entry': plain_text, 'conv_decrypt': True, 'conv_encrypt': False}
        return render(request, 'landings/home.html', context)
    
    elif 'conversion' in request.GET:
        conversion = request.GET['conversion']
        if conversion == 'encrypt':
            context = {'select_option': False, 'result_encrypt': True, 'result_decrypt': False, 'conv_decrypt': True, 'conv_encrypt': False, 'conv_decrypt': False, 'conv_encrypt': True}
            return render(request, 'landings/home.html', context)
        elif conversion == 'decrypt':
            context = {'select_option': False, 'result_encrypt': False, 'result_decrypt': True}
            return render(request, 'landings/home.html', context)
    
    return render(request, 'landings/home.html')

这是我正在使用的模板 (home.html):

{% block content %}   

   <br>
   <center>
  
   <h1>Hello, World!</h1>
   {% if select_option %}
   <h3>Would you like to <a href="{% url 'caesar' %}?conversion=encrypt">encrypt</a> a plaintext message?</h3>

   <h3>Or would you like <a href="{% url 'caesar' %}?conversion=decrypt">to decrypt</a> a scrambled message?</h3>
   {% elif conv_encrypt %}
   <form action="{% url 'caesar' %}" >
       Enter the text you wish to scramble here:
       <br>
       <input type="text" name="original_entry" />
       <br />
       Enter your key:
       <br>
       <input type="number" name="shift_num" />
       <br />
       <input type="submit" value="Encrypt!" />
     </form>
   </form>
   {% elif conv_decrypt %}
   <form action="{% url 'caesar' %}" >
       Enter the text you wish to scramble here:
       <br>
       <input type="text" name="original_entry" />
       <br />
       Enter your key:
       <br>
       <input type="number" name="shift_num" />
       <br />
       <input type="submit" value="Decrypt!" />
     </form>
   {% elif result_encrypt %}
  
   <h3>Here is your message encrypted:</h3>

   <div class="field"> {{ output_text }} </div>

   <br>
   <a href="{% url 'caesar' %}"><div class="field"> <strong> Again? Click here! </strong> </div> </a>

 {% elif result_decrypt %}
   <h3>Here is your message decrypted:</h3>
  
   <div class="field"> {{ output_text }} </div>
 
   <br>
   <a href="{% url 'caesar' %}"><div class="field"> <strong> Again? Click here! </strong> </div> </a>

 {% endif %}

这是我完整的 GitHub 仓库中的 the full source code,参考了我当前的工作分支。

到目前为止我尝试过的:

我的本地开发环境是 运行 Python 3.9.1 Linux 系统。这是 $ pip freeze 的输出:

asgiref==3.3.1 
Django==3.1.5 
Pillow==8.1.0 
pytz==2020.5
sqlparse==0.4.1 
whitenoise==5.2.0

我将此凯撒密码 Web 应用程序基于一个名为 CC_Redact which successfully serves the web visitor a choice either to convert miles to kilometres or kilometres to miles (template, views.py) 的独立 Django 项目的先前 Web 应用程序。它的功能和行为符合预期。我正在尝试模拟该工作功能。

至于我,你所有的问题是你必须 运行 url 带参数才能看到任何东西

d0d342ea4934.ngrok.io/?conversion=encrypt 
d0d342ea4934.ngrok.io/?conversion=decrypt

d0d342ea4934.ngrok.io/?encrypt&original_entry=...&shift_num=...
d0d342ea4934.ngrok.io/?decrypt&original_entry=...&shift_num=...

当你 运行 它没有参数时

d0d342ea4934.ngrok.io

然后 运行 最后 return

return render(request, 'landings/home.html')

没有上下文 {'select_option': True, ...} 所以你的 template 什么都不显示。

最后 return 你应该至少

return render(request, 'landings/home.html', {'select_option': True})

显示

Would you like to encrypt a plaintext message? 
Or would you like to decrypt a scrambled message?