I have some problems with Django models, I would like to iterate data got from the textfield from the form based on model. But I can't it still an error:
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
Code 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})
I would be grateful for help!!
Natalia
CodePudding user response:
The text
is a Password
object, not a string. You should iterate over the password so:
password_form = PasswordsForm(request.POST or None)
if password_form.is_valid():
password = password_form.save()
try:
for i in password.text:
# …
except KeyError:
# …