从一个视图移动到另一个视图时,后一个视图不会被处理

时间:2014-02-07 09:10:15

标签: django django-forms django-templates django-views

所以我有一个dlist视图,显示一个带有复选框的字段列表,可供选择。所选字段将通过提交按钮发送的整行返回到编辑模板以进行进一步处理。这是dlist视图及其形式:

def dlist(request):
...
if "_edit" in request.POST:
    print "You pressed update button in list template"
    form = selectForm2(request.POST)
    if form.is_valid(): # All validation rules pass
        print "selectForm VALID!"
        delete_items = form.cleaned_data['select_fields']
        for item in delete_items:
            instance = model_class.objects.get(**{field_list[2]:item})
            instance_list.append(item)      
        messages.success(request, 'Selected fields updated')
        return render(request, 'Directories/edit.html', {"field_names": field_names, "instance_list": instance_list, ...})
    else:
            return HttpResponse('ERROR in POST -- Return to form submission')

class selectForm2(forms.Form):
    select_fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=fields())

# the choices for the form. It is a list of field ('descr_en') values.
def fields():
    m_values = Attributes.objects.values_list('descr_en', flat=True)
    for val in m_values:
        field_classes.append((val, val))
    return field_classes

及其模板

*list.html*
...
<form action="" method="post">  {% csrf_token %}
<tbody>
{% with field_names|slice:"1:" as sliced_fnames %}      
    <tr>
    {% for f_name in sliced_fnames %}
            {% if f_name = sliced_fnames.0 %}
                <td>
                   {{form.as_p}} 
                </td>   
            {% endif %}
        {% endfor %}

{% endwith %}
</tbody>
</table> <br />
<input type="submit" value="Update selected items" name="_edit" />
</form>

现在当按下“_edit”提交按钮时,我应该被重定向到编辑模板和modelEdit视图,并看到打印的第一条消息如下所示:

def modelEdit(request):
    print "seems like u pressed the edit button!"
    updated_item = request.POST.get('select_fields')
    print "updated_item", updated_item
    ...
    return render(request, 'Directories/edit.html')

然而,这不会发生,也不会显示任何内容。仍然,从dlist视图(field_names,instance_list)传递到selectForm的字典在edit.html中呈现:

*edit.html*
<div id="content" align="center">
<h3> Update entry</h3> 
<br />
{{m_tb_name}}
{% if instance_list %}
    {% for instance in instance_list %}
        {{instance}}
    {% endfor %}
{% endif %}
<form action="" method="post"> {% csrf_token %}
    {% for f_name in field_names %} 
        {% if not forloop.first %}      
           {% for instance in instance_list %}
                {{f_name}}: <input id="edit-{{f_name}}" value="{{instance}}" type="text" name={{f_name}} /><br />
           {% endfor %}
        {% endif %}
    {% endfor %}<br />
    <input type="submit" name="_alter" value="Update" />
    <input type="reset" name="Clear" value="Clear" />
</form> 
</div>

最后,这是我的urls.py文件:

*urls.py*
from django.conf.urls import patterns, url
from Directories import views

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^list$', views.dlist, name='list_models'),
url(r'^edit$', views.modelEdit, name='edit_models'),
...
)

因此,当我在浏览器中手动编辑URL作为目录/编辑时,我会从我的视图中看到打印的消息,但当我尝试通过list.html中的“_edit”按钮访问edit.html时,它会呈现编辑模板但由于某种原因不处理视图(modelEdit)。

1 个答案:

答案 0 :(得分:1)

您只是渲染Directories / edit.html,而不是调用视图。您可以使用modelEdit中的print语句验证它。 相反,你可能需要的是

return HttpResponseRedirect('/edit')

这将调用您的观点。