在Django中渲染FormSet

时间:2019-06-26 15:13:53

标签: python html django

我想创建一个FormSet,允许用户根据需要添加表单。但是,当我渲染页面时,我收到一个错误:

ValueError: The view website.views.presales didn't return an HttpResponse object.
  It returned None instead.

我想始终将表单空白。请让我知道是否需要其他信息,谢谢!

注意:cwObj.get_opportunities()是一个API调用,用于根据JSON响应创建对象以填充select_opportunity下拉列表。最后,我使用AJAX通过Total动态计算跨度data-total-url="{% url 'presales_total' %}"的值。

forms.py

class PresalesForm(forms.Form):
    class Meta:
        model = Presales
        fields = ('selected_opportunity', 'task_description', 'hours', 'selected_engineer_level', 'total_cost')

views.py

def presales(request):
    my_opportunities = cwObj.get_opportunities()

    PresalesFormSet = formset_factory(PresalesForm, extra=1)

    if request.method == 'POST':
        presales_formset = PresalesFormSet(request.POST)

        if presales_formset.is_valid():

            for presales_form in presales_formset:
                selected_opportunity = request.POST.get('selected_opportunity')
                task_description = request.POST.get('task_description')
                hours = request.POST.get('hours')
                select_engineer_level = request.POST.get('select_engineer_level')

            else:
                presales_formset = PresalesFormSet(initial="None")

                context = {'presales_formset': presales_formset, 'my_opportunities': my_opportunities}

            return render(request, 'website/presales.html', context)

presales.html

<form action="{% url 'presales' %}" method="post" name="presalesForm" id="presalesForm" data-total-url="{% url 'presales_total' %}">
                {% csrf_token %}

                {{ presales_formset.management_form }}

                {% for presales_form in presales_formset %}
                <div class="field">
                    <label class="label is-large">Create Task</label>
                </div>
                <div class="section">
                    <div class="field">
                        <label class="label">Opportunity</label>
                        <div class="select">
                            <select name="select_opportunity" id="select_opportunity">
                                <option value="">Opportunity</option>
                                {% for opportunity in my_opportunities %}
                                <option name="selected_opportunity" id="selected_opportunity" value="{{ opportunity.name }}">{{ opportunity.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <label class="label">Task Description:</label>
                    <div class="field">
                        <div class="control">
                            <input class="input" name="task_description" id="task_description" placeholder="Task Description">
                        </div>
                    </div>
                    <label class="label">Hours</label>
                    <div class="field">
                        <div class="control">
                            <input class="input" name="hours" id="hours" placeholder="Hours">
                        </div>
                    </div>
                    <label class="label">Engineer Level:</label>
                    <div class="field">
                        <div class="select">
                            <select name="select_engineer_level" id="select_engineer_level">
                                <option value="">Engineer Level</option>
                                <option value="PM">PM</option>
                                <option value="Solutions Technician">Solutions Technician</option>
                                <option value="Solutions Engineer">Solutions Engineer</option>
                                <option value="Senior Solutions Engineer">Senior Solutions Engineer</option>
                                <option value="Solutions Architect">Solutions Architect</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="field">
                    <div class="control">
                        <button class="button is-info" type="button">Add Task</button>
                    </div>
                </div>
                {% endfor %}
                <span class="label is-medium is-pulled-right" id="total_cost">Total: {{ total }}</span>
            </form>

1 个答案:

答案 0 :(得分:1)

您的视图在所有情况下都不会返回HttpResponse,这是一个错误。当request.method与POST不同且表单集无效时,就会发生这种情况。

在删除大部分代码并仅保留条件语句时,这很容易发现:

def presales(request):
    if request.method == 'POST':
        if presales_formset.is_valid():
            ...
            return render(request, 'website/presales.html', context)

在其他情况下,您还必须返回其他HttpResponse来解决此错误。

例如,您可以将return render(...行的2个级别取消缩进,以便视图始终返回模板。