首页 > 解决方案 > 无法将表单信息发送到 Django 中的函数

问题描述

请看我的代码 我创建了一个类来创建评论,我还写了 Vivo 和 HTML 代码,但是无论我做什么,信息都没有注册。有谁知道原因吗??

在模型管理中查看功能注册评论

 def comment_view(request):
    comment_form = CommentForm(request.POST or None)
    if comment_form.is_valid():
        title_comment = comment_form.cleaned_data.get('title_comment')
        full_name = comment_form.cleaned_data.get('full_name')
        comment_text = comment_form.cleaned_data.get('comment_text')
        strengths = comment_form.cleaned_data.get('strengths')
        weak_points = comment_form.cleaned_data.get('weak_points')
        product_id = comment_form.cleaned_data.get('product_id')
        product: Product = Product.objects.get_by_id(product_id)
        CommentModel.objects.create(owner_id=request.user.id, product_id=product.id, title_comment=title_comment,
                                    full_name=full_name, comment_text=comment_text, strengths=strengths,weak_points=weak_points
                                    )
        messages.success(request,'نظر شما با موفقیت ثبت شد')
        return redirect('/')

    context = {'comment_form': comment_form, 'title': 'ایجاد نظر جدید'}
    return render(request, 'comment.html', context)

模型评论

class CommentForm(forms.Form):
    full_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نام و نام خانوادگی خود را وارد کنید'}))
    title_comment = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'عنوان نظر خود را وارد کنید'}))
    comment_text = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'متن نظر خود را وارد کنید'}))
    strengths = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نقاط قوت محصول  را وارد کنید'}))
    weak_points = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نقاط ضعف محصول را وارد کنید'}))
    product_id = forms.IntegerField(widget=forms.HiddenInput())

网址评论

 path('new-comment', comment_view, name='new-comment'),

表格评论

class CommentForm(forms.Form):
    full_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نام و نام خانوادگی خود را وارد کنید'}))
    title_comment = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'عنوان نظر خود را وارد کنید'}))
    comment_text = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder' : 'متن نظر خود را وارد کنید'}))
    strengths = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نقاط قوت محصول  را وارد کنید'}))
    weak_points = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'نقاط ضعف محصول را وارد کنید'}))
    product_id = forms.IntegerField(widget=forms.HiddenInput())

html文件注释

  <form method="post">
                                    {% csrf_token %}
                                    <div class="row">
                                        <div class="col-12">
                                            <div class="form-row-title mb-2">نام و نام خانوادگی (اجباری)</div>
                                            <div class="form-row">
                                                {{comment_form.full_name}}
                                            </div>
                                        </div>
                                        <div class="col-12">
                                            <div class="form-row-title mb-2">عنوان نظر شما (اجباری)</div>
                                            <div class="form-row">
                                                {{comment_form.title_comment}}
                                            </div>
                                        </div>
                                        <div class="col-12 form-comment-title--positive mt-2">
                                            <div class="form-row-title mb-2 pr-2">
                                                نقاط قوت
                                            </div>
                                            <div id="advantages" class="form-row">
                                                <div class="ui-input--add-point">
                                                    {{comment_form.strengths}}
                                                    <button class="ui-input-point js-icon-form-add"
                                                        type="button"></button>
                                                </div>
                                                <div class="form-comment-dynamic-labels js-advantages-list"></div>
                                            </div>
                                        </div>
                                        <div class="col-12 form-comment-title--negative mt-2">
                                            <div class="form-row-title mb-2 pr-2">
                                                نقاط ضعف
                                            </div>
                                            <div id="disadvantages" class="form-row">
                                                <div class="ui-input--add-point">
                                                    {{comment_form.weak_points}}
                                                    <button class="ui-input-point js-icon-form-add"
                                                        type="button"></button>
                                                </div>
                                                <div class="form-comment-dynamic-labels js-disadvantages-list"></div>
                                            </div>
                                        </div>
                                        <div class="col-12 mt-5">
                                            <div class="form-row-title mb-2">متن نظر شما (اجباری)</div>
                                            <div class="form-row">
                                                {{comment_form.comment_text}}
                                            </div>
                                        </div>
                                        <br>
                                        <br>
                                        <br>
                                        <div class="col-12 px-0">
                                            <button type="submit" class="btn btn btn-primary px-3">
                                                ثبت نظر
                                            </button>
                                        </div>
                                    </div>
                                </form>

html代码在详细页面

 // detail page
                                    <form method="post" action="{% url 'new-comment' %}">
                                        {% csrf_token %}
                                        {{  comment_form.product_id }}
                                        <button type="submit" class="btn-primary-cm btn-with-icon">
                                       ایجاد نظر
                                    </button>
                                    </form>

标签: djangoformswebmodelcomments

解决方案


尝试做

commentobject=CommentModel.objects.create(owner_id=request.user.id, product_id=product.id, title_comment=title_comment,
                                full_name=full_name, comment_text=comment_text, strengths=strengths,weak_points=weak_points
                                )
commentobject.save()

它可能会解决问题(虽然不确定)。


推荐阅读