我有一个一直在工作的搜索页面,直到我将返回的代码进行编码以进行编辑。
理想情况下,用户进行搜索并返回一些值,这些值到目前为止都很好。然后,当用户单击编辑按钮以获取值时,我希望字段以可编辑的格式返回。

这是views.py

def search_device_list(request, id):
     license_key = Maintenance.objects.get(id=id)
     maintenance_form = MaintenanceForm(instance=license_key)
     devices = Devices.objects.filter(maintenance = id)
     end_of_maintenance_support = MaintenanceForm()

     locations = Locations.objects.all()
     context ={"locations": locations}

     for locations in context['locations']:
        print(locations)

     if request.method == 'POST':
         location_name=request.POST.get('location_name')
         my_devices = Devices.objects.filter(locations = Locations.objects.get(location_name = location_name))
         maintenance_form = MaintenanceForm(request.POST, instance=license_key)
         license_key = request.POST.get('license_key')
         maintenance_support_end_date = request.POST.get('maintenance_support_end_date')

         context["devices"]= my_devices
         context["location_name"]= location_name
         context["license_key"] = license_key
         context["maintenance_support_end_date"] = maintenance_support_end_date

         if maintenance_form.is_valid():
            license = maintenance_form.save()
            print (license.license_key)

            postdevices= request.POST.getlist("devices")
            for device_hostname in postdevices:
                device = Devices.objects.get(device_hostname = device_hostname)
                print(device.maintenance.license_key)
                print(device.maintenance.maintenance_support_end_date)
                device.save()

     return render(request, 'inventory/search_device_list.html', context)

这是template.py
{% block content %}

<h2 align="left">Search Page</h2>

<!-- <form action="{% url 'inventory:render_results' %}" method="POST" > -->
<form action="{% url 'inventory:search_device_list' %}" method="POST" >

{% csrf_token %}
<body>
<table class=table>
<tr>
    <td><b>Locations: &nbsp;&nbsp; </b></td>
    <td>
    <select name="location_name">
    {% for locations in locations %}
    <option value="{{locations.location_name}}">{{locations.location_name}}</option>{% endfor%}
    </select>
    </td>
    <td><input type="submit" name = "submit" value="Submit"/></td>
</tr>
<br>

<h3 align="left">Render Device List based on Location Choice</h3>
<table>
    <tr>
        <td><b>Locations:</b><br>{{location_name }} <br></td>
        <td><b> Devices: </b><br>
        {% for device in devices %}{{device.device_hostname}} <br>{% endfor %}  </td>

        <td id="maintenance_support_end_date"><b>End of Maintenance Support:</b><br>{% for device in devices%}{{device.maintenance.maintenance_support_end_date}}<br> {% endfor %}  </td>
        <td id="license_key"><b>License Key:</b><br>{% for device in devices%}{{device.maintenance.license_key}}<br>{% endfor %}</td>
        <td><b>Actions:</b><br> {% for device in devices%} <a href= {% url 'inventory:search_device_list' %}><button id="maintenance_support_end_date">Edit </button></a><br> {% endfor %}  </td>

    </tr>
</table>

<table class=table>
<h3 align="left"> Edit Table </h3>
<tr>
    <td><b>Locations:&nbsp;</b></td><td>{{maintenance_form.location_name}}</td>
    <td><b>Devices&nbsp;</b></td><td>{% if devices %}
                {% for device in devices %}
                    {{ device }} <br><br>
                {% endfor %} </td>
    <td><b>License Key:&nbsp;</b></td><td>{{maintenance_form.license_key}}</td>
    <td><b>Support End Date:&nbsp;</b></td><td>{{maintenance_form.maintenance_support_end_date}}</td>
</tr>
</table>

 {% endblock content %}

最后,urls.py-我确实对此错误消息进行了一些搜索,因为它很常见,所以我尝试了两个不同的url路径
     url(r'^search_device_list/$', views.search_device_list, name='search_device_list'),
     url(r'^search_device_list/(?P<id>\d+)$', views.search_device_list, name='search_device_list'),

这里引用了两个不同的模型,即设备和维护。我认为这是我对id如何通过代码传递的理解的一个缺陷。任何想法或提示表示赞赏。

最佳答案


更改def search_device_list(request, id):def search_device_list(request, id=None):请注意,您必须修改代码,以便处理idNone时的情况,因为当前,您的 View 取决于所提供的id
说明/search_device_list//search_device_list/(?P<id>\d+)都使用相同的 View 。
当您访问第一个URL时,它会调用您的 View 函数,但不会传递id
当您访问/search_device_list/(?P<id>\d+)时,它会调用您的view函数,但是这次它传递了id
通过将view函数更改为def search_device_list(request, id=None):,您可以将view函数的id参数设置为可选

关于django - Django : search_device_list() missing 1 required positional argument: 'id' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60895151/

10-11 22:08
查看更多