我有一些Django模型的问题,我想迭代从基于模型的表单的文本字段得到的数据。但是我不能,它仍然是一个错误:
TypeError at /kod/start/
'Passwords' object is not iterable
Request Method: POST
Request URL: http://127.0.0.1:8000/kod/start/
Django Version: 3.2.8
Exception Type: TypeError
Exception Value:
'Passwords' object is not iterable
Exception Location: /home/natalia/PycharmProjects/django_project/kodszyfru/views.py, line 52, in start
Python Executable: /home/natalia/PycharmProjects/django_project/venv/bin/python3
Python Version: 3.8.10
Python Path:
['/home/natalia/PycharmProjects/django_project',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/natalia/PycharmProjects/django_project/venv/lib/python3.8/site-packages']
Server time: Fri, 22 Oct 2021 16:03:53 +0000
代码models.py
from django.db import models
# Create your models here.
class Passwords(models.Model):
text = models.TextField(max_length=2000,null=True)
cipher = models.CharField(max_length=64)
def __str__(self):
return self.text
views.py
def start(request):
password_form = PasswordsForm(request.POST or None)
if password_form.is_valid():
text = password_form.save()
try:
for i in text:
value = (alphabet[i])
klucz = 10
new_value = int(value) + klucz
if new_value > 70:
new_value = new_value - 70
for key, value in alphabet.items():
if value == str(new_value):
print(key, end='')
return ''
except KeyError:
return ('Nie znaleziono wartości' + i + ' w słowniku!')
return render(request, 'kod.html', {'kod': kod})
return render(request, 'start.html', {'password_form': password_form})
我将感激你的帮助!
纳塔莉亚
text
是Password
对象,而不是字符串。您应该遍历密码,以便:
password_form = PasswordsForm(request.POST or None)
if password_form.is_valid():
password = password_form.save()
try:
for i in password.text:
# …
except KeyError:
# …