Функция is_valid (), возвращающая false на основе обратной трассировки из IntergerField в формах. Я, вероятно, упускаю что-то в строке кода is_valid (). Любой вклад приветствуется.
traceback
[27/Apr/2020 02:23:07] "GET / HTTP/1.1" 200 10413
5
<bound method BaseForm.is_valid of <IdForm bound=True, valid=Unknown, fields=(id)>>
<tr><th><label for="id_id">Song ID:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="id" required id="id_id"></td></tr>
<tr><th><label for="id_id">Song ID:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="id" required id="id_id"></td></tr>
<bound method BaseForm.is_valid of <IdForm bound=False, valid=Unknown, fields=(id)>>
[27/Apr/2020 02:23:09] "POST /playlist/add/5/ HTTP/1.1" 200 4406
forms.py
class IdForm(forms.Form):
id = forms.CharField(label='Song ID', required=True)
template
<form action="{% url 'playlist_add' P_id=p.id %}" method="POST" enctype="multipart/form-data" value="{{request.post.id}}">
{% csrf_token %}
{%for field in form%}
{{field.errors}}
{{field}}
{%endfor%}
<button type="submit">Add Song</button>
</form>
views.py
class PlaylistAddFormFunction(View):
form_class = IdForm #determine fields
template = 'homepage_list.html'
def get(self, request):
form = self.form_class(None)
print('soo')
return render(request, self.template, {'form':form})
@method_decorator(login_required)
def post(self, request, P_id):
print(P_id)
form = IdForm(request.POST)
print(form.is_valid)
print(form)
if form.is_valid():
print('xoo')
id = form.cleaned_data['id']
song = Song.objects.get(id=id)
playlist, created = Playlist.objects.get_or_create(id=P_id)
playlist.song.add(song)
return redirect('home')
else:
print(form)
form = self.form_class(None)
print(form.is_valid)
return render(request, self.template, {'form':form})