Django-在模板中处理多个表单请求

时间:2018-09-17 20:55:15

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

我可能没有措辞最好的问题,但是我想知道什么是处理模板中多个请求可能性的最佳/最佳方法?

例如我有一个模板,用于显示表单以及保存在单独表中但与当前记录直接相关的所有附件。因此,我有一个用于表单的提交按钮,还有两个其他按钮,用于从单独的表中添加和删除附件。

我已经通过利用name标签上的input属性来处理附件的添加。最初,我想与删除做同样的事情,但是我没有办法将记录的ID传递给view函数,因此我创建了一个单独的URL来处理该请求。这种方法的问题在于,我无法再将相同的表单实例传递回用户。用户点击删除后,它将基本重置表单。

创建一些仅用于处理请求的URL是否是典型/首选? 我觉得最好的答案是使用AJAX请求,但是出于性能原因,在尝试进行更改之前,我试图使该功能正常工作。

我将下面的全部功能包括在内,但我只是担心请求的处理方式以及是否应该以其他方式处理。

Views.py

def create_req(request):
    all_attachments = TempAttachments.objects.filter(username=request.user)

    if request.method == 'POST':
        req_form = ReqForm(request.POST, request.FILES)

        if request.POST.get("Add") and len(request.FILES) != 0:
            add_attachment = TempAttachments.objects.create(attachment=request.FILES['attachment'], username=request.user)

        elif req_form.is_valid():
            cur_user = CustomUser.objects.get(username=request.user)
            basket_item = ReqBasket.objects.create(user=cur_user,
                                description = req_form.cleaned_data['description'].capitalize(),
                                extra_information = req_form.cleaned_data['extra_information'],
                                quantity = req_form.cleaned_data['quantity'],
                                price = req_form.cleaned_data['price'],
                                total_price = req_form.cleaned_data['quantity']*req_form.cleaned_data['price'],
                                assistance = req_form.cleaned_data['assistance'],
                                catagory = req_form.cleaned_data['catagory'])

            return redirect(reverse('requisition:basket'))
        else:
            req_form = ReqForm(request.POST)
    else:
        req_form = ReqForm()

    return render(request, 'req/create_req.html', {'req_form': req_form, 'all_attachments': all_attachments})

def remove_temp_attachment(request, id):
    attachment = get_object_or_404(TempAttachments, id=id, username=request.user)
    if request.method == 'POST':
        req_form = ReqForm(request.POST, request.FILES)
        attachment.delete()
        return redirect(reverse('requisition:create_req'), {'req_form':req_form})
    else:
        return redirect(reverse('requisition:create_req'))

Template.py-对不起,无法启动引导程序

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% extends 'registration/home.html' %}
{% load static %}

{% block content %}
<h1>New Requisition</h1>
    
<form method="post" action="{% url 'requisition:create_req' %}" enctype="multipart/form-data">
    {% csrf_token %}
    <table>
    {% for field in req_form.visible_fields %}
        {% ifnotequal field.label 'Notes' %}
        {% if field.label == 'Attachment' %}
        <tr> 
            <th>{{ field.label_tag }}</th>
            <td>
                {{ field }}
            </td>
            <td>
                <input type="submit" value="Add" name="Add">
            </td>
        </tr>
        {% else %}    
        <tr> 
            <th>{{ field.label_tag }}</th>
            <td>
                {{ field }}
            </td>
        </tr>
        {% endif %}
        {% endifnotequal %}
    {% endfor %}
    </table>
    <input type="submit" value="Add To Basket">
</form>

<a href="{% url 'requisition:basket' %}">Back</a>

<table class='table'>
    {% if all_attachments %}
    {% for attachment in all_attachments %}
    <tr>
        <td>
            <a href="{% get_media_prefix %}/{{attachment.attachment}}">{{attachment}}</a>
        </td>
        <td>
            <form action="{% url 'requisition:remove_temp_attachment' attachment.id %}" method="POST">
                {% csrf_token %}
                <input type="submit" value="Delete">
            </form>      
        </td>
    </tr>
    {% endfor %}
    {% endif %}
</table>


{% endblock content %}

0 个答案:

没有答案