У меня есть форма с некоторыми Флажки и какой-то текст, когда пользователь представить данные, которые я с помощью следующей формы объекта:Не удалось получить пост данные в multipleChoiceField с Джанго
class PostForm(forms.Form):
product = forms.MultipleChoiceField(
label='product',
widget=forms.CheckboxSelectMultiple)
text = forms.CharField(label='text', max_length=1000)
def __init__(self, choices, *args, **kwargs):
super(PostForm, self).__init__(*args, **kwargs)
self.fields['product'].choices = choices
Но когда я отладки кода , пост cleaned_data
совершенно пуст, и data
объект содержит все данные:
<QueryDict: {'text': ['Hello world, this is a test\r\n'], 'product': ['7', '9'], 'csrfmiddlewaretoken': ['QYuwBoW3O5D42oScF2GzYuesTBIZZqRa']}>
Update:
Здесь шаблон:
<h1>Create post</h1>
<textarea rows="10" cols="50" name="text" form="post_form">
Hello world, this is a test
</textarea>
<form action="" method="post" id="post_form">
{% csrf_token %}
<label for="product_id" id="product_id">products: </label>
{% for product in products %}
<input type="checkbox" name="product" value="{{product.id}}">{{product.name}}
{% endfor %}
</select>
<br>
<input type="submit" value="OK">
</form>
А вот вид:
class PostView(AuthenticatedUserView):
template_name = 'trendby/post.html'
def get_if_authenticated(self, request, user):
products = models.Product.objects.filter(user=user)
return render(request, self.template_name, {'products': products})
def post_if_authenticated(self, request, user):
products = models.Product.objects.filter(user=user)
choices = []
for product in products:
choices += [str(product.id)]
form = PostForm(choices, request.POST)
if form.is_valid():
text = form.cleaned_data['text']
post = models.Post(text=text, user=user)
post.save()
return HttpResponse("Post: " + text)
return render(request, self.template_name)
Может кто-нибудь сказать мне, что я могу сделать, чтобы исправить `cleaned_data '?
Спасибо!
Что на ваш взгляд выглядит? –
Я только что обновил сообщение –