在django中从“表单动作”发送数据到视图函数

au9on6nz  于 2022-12-30  发布在  Go
关注(0)|答案(1)|浏览(150)

大家好,我有点困惑我的新手和缺乏知识之间,我在Django的一个小项目工作,我也试图发送数据从一个表单动作在html到另一个视图函数,但我不理解它好这是如何工作的,在这之上,我必须发送几个数据,而不仅仅是一个,它让我更困惑,我有以下HTML:

{% extends "base.html" %}

{% block content %}

<main class="container">
    <div class="row">
        <div class="col-md-10 offset-md-1 mt-5">
            <form action="/interface/" method="POST" class="card card-body">

                <h1>Interface</h1>
                <h4>{{ error }}</h4>
                <select  name="dv">
                    <option selected disabled="True">Select Device</option>
                    {% for device in devicess %}
                        <option>{{ device.id }} - {{ device.name }}</option>
                    {% endfor %}
                </select>
                <br>
                {% csrf_token %}

                <br>
                <button type="submit" class="btn btn-primary">Send</button>
            </form>
                <br>
            {% for interface in interfaces %}       
            <section class="card card-body">
                
                <h2>{{interface.Interface}}</h2>

                {% if interface.Description == "" %} 
                    <p class="text-secondary">none description</p>
                {% else %} 
                    <P class="text-secondary">{{interface.Description}}</P>                     
                {% endif %}
            
                <form  action= "{% url 'send_description' %}"method="POST">
                    {% csrf_token %}
                    <input type="text" name="command" class="form-control" placeholder="Change description">
                    <br>
                    <button type="submit" class="btn btn-primary align-content-lg-center">Send change</button>
                </form>

                <br>

                {% if interface.Status == "up" %} 
                    <p class="text-secondary">Interface State: 🟢 Free</p>
                {% else %} 
                    <p class="text-secondary">Interface State: 🔴 Used</p>
                {% endif %}

                
            </section>
                <br>
            {% endfor %}    
            
        </div>
    </div>
</main>

{% endblock %}

从美学Angular 更好地理解执行的第一次POST,如下所示:

到目前为止一切都很完美,如果我按下“发送更改”按钮,它会完美地重定向我,问题是我需要发送各种数据,如device.id,接口到我正在执行的函数中的action=“{% url 'send_description' %} .接口,以及输入的内容,这是在同一个表单。你能给予我一把或告诉我在哪里能找到最好的路吗?
问候!

tcomlyy6

tcomlyy61#

让我先说这在JS和 AJAX 中会更好,但是,为了回答你的问题,数据是通过Django http request object传递的,在你的例子中,因为你有几个不同的表单,所以可以通过在每个表单中添加一个隐藏字段来传递数据:

<input type="hidden" name="interface" value="{{ interface.id }}">

并从视图中的request对象获取该值:

interface = request.POST.get('interface')

完整示例:
models.py

class Device(models.Model):
    name = models.CharField(max_length=100)

class Interface(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=100, default='interface description field')
    status = models.BooleanField(default=False)
    device = models.ForeignKey(Device, on_delete=models.CASCADE, related_name='interfaces')

views.py

from django.core.exceptions import ObjectDoesNotExist

def list_interfaces(request):
    devices = Device.objects.all()
    interfaces = None

    try:
        selected_device = Device.objects.get(id=request.POST.get('dv'))
        interfaces = selected_device.interfaces.all()

    except ObjectDoesNotExist:
        selected_device = Device.objects.all().first()
        if selected_device:
            interfaces = selected_device.interfaces.all()
        else:
            selected_device = None

    context = {
        'devices': devices,
        'selected_device': selected_device,
        'interfaces': interfaces
    }
    return render(request, 'list_device_interfaces.html', context)

def send_description(request):
    command = request.POST.get('command')
    device = request.POST.get('seleted_device')
    interface = request.POST.get('interface')
    print(f'command: {command}')
    print(f'device_id: {device}')
    print(f'device_id: {interface}')
    return redirect('core:list-device-interfaces')

urls.py

from core import views
from django.urls import path

app_name = 'core'

urlpatterns = [
    path("list/device/interfaces/" , views.list_interfaces, name="list-device-interfaces"),
    path("send/description/" , views.send_description, name="send-description"),
]

list_device_interfaces.html

{% extends "base.html" %}

{% block content %}

<main class="container">
    <div class="row">
        <div class="col-md-10 offset-md-1 mt-5">
            <form action="{% url 'core:list-device-interfaces' %}" method="POST" class="card card-body">
                {% csrf_token %}
                <h1>Device</h1>
                <h4>{{ error }}</h4>
                <select  name="dv">
                    <option selected disabled="True">Select Device</option>
                    {% for device in devices %}
                        <option value="{{ device.id }}" {% if device.id == selected_device.id %} selected {% endif %}>{{ device.id }} - {{ device.name }}</option>
                    {% endfor %}
                </select>
                <br>
                <br>
                <button type="submit" class="btn btn-primary">Send</button>
            </form>
                <br>
            <hr>
            <h2>Interfaces</h2>
            {% for interface in interfaces %}       
            <section class="card card-body">
                
                <h2>{{interface.name}}</h2>

                {% if interface.description == "" %} 
                    <p class="text-secondary">none description</p>
                {% else %} 
                    <P class="text-secondary">{{interface.description}}</P>                     
                {% endif %}
            
                <form  action= "{% url 'core:send-description' %}"method="POST">
                    {% csrf_token %}
                    <input type="text" name="command" class="form-control" placeholder="Change description">
                    <input type="hidden" name="seleted_device" value="{{ selected_device.id }}">
                    <input type="hidden" name="interface" value="{{ interface.id }}">
                    <br>
                    <button type="submit" class="btn btn-primary align-content-lg-center">Send change</button>
                </form>

                <br>

                {% if interface.status %} 
                    <p class="text-secondary">Interface State: 🟢 Free</p>
                {% else %} 
                    <p class="text-secondary">Interface State: 🔴 Used</p>
                {% endif %}

                
            </section>
                <br>
            {% endfor %}
            
        </div>
    </div>
</main>

{% endblock %}

相关问题