Django;将值从下拉列表传递到主页

时间:2020-02-24 20:21:55

标签: python django django-templates html.dropdownlistfor

我仍然是django的新手,并且我有一个带有下拉菜单的表单;我可以做出选择,但是一旦做出选择,我需要将其传递到主页的帮助。我认为我需要在视图中添加细节,但是我不确定是否在其中,或者是否需要将细节添加到* .html主页中

views.py

// When left click is pressed, a raycast is sent out. If that raycast hits the wall, updatePoints() is called and is passed in the location of the hit (hit.point).
public void updatePoints(Vector3 pos)
{
    // Convert pos to local coords of wall. You don't need to do this, i do it because of my draw() method where i map everything out onto a texture and display it.
    pos = transform.InverseTransformPoint(pos);
    pos.x = map(pos.x, -0.5f, 0.5f, 0, 512);
    pos.y = map(pos.y, -0.5f, 0.5f, 0, 512);
    Vertex newVert = new Vertex(pos.x, pos.y);

    // Manually add new vertex to list of vertices.
    newVert.id = mesh.vertices.Count;
    mesh.vertices.Add(newVert.id, newVert);

    //Doing just the first line gave me a null pointer exception. Adding the two extra lines below it fixed it for me.
    otri.tri = mesh.dummytri;
    otri.orient = 0;
    otri.Sym();

    // Insert new vertex into existing triangulation.
    mesh.InsertVertex(newVert, ref otri, ref osub, false, false);

    // Draw result as a texture onto the wall so to visualise what is happening.
    draw();
}

urls.py

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

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

    if request.method == "POST":
        location_name = request.POST.get("locations")   

    return render(request, 'example/search_device.html', context, )

device_list.html

urlpatterns = [
path('admin/', admin.site.urls),
path('', example_views.home, name="home"),
path(r'^location/$', example_views.home, name="location_name"),
path('add_location', example_views.add_location, name='add_location'),
path('add_device', example_views.add_device, name='add_device'),
path('search_device', example_views.search_device, name='search_device')]

和search_devices.html

<html>
<head>
    <title>Device List</title>
</head>
<form method="post">
<body>
    <a href="add_device">Add Device</a>   <a href="add_location">Add Location</a>   <a 
     href="search_device">Search Devices</a>
<p>
<h3>Devices Go Here:</h3>
{% csrf_token %}

{% for device in devices %}
        {{device.device_name}} <br>
{% endfor %}        

<br>
<br>

<h3>Associated Locations</h3>
{% if location.location_name %}
<p> Great, you selected: {{location.location_name}}
</p>
{% endif %}

</body>
</html>

2 个答案:

答案 0 :(得分:0)

对于您的问题,我可以参考一下,您可能需要更新带有名称和选项标签的html文件。

https://www.youtube.com/watch?v=2gmS4LAHTOM

希望有帮助,

答案 1 :(得分:0)

views.py需要以下附加代码:

 def device_list(request):
    location_name = request.POST.get('location_name')
    my_devices = Devices.objects.filter(location= Locations.objects.get(location_name = location_name))

context = {"devices": my_devices, "locations": location_name} return render(request, 'example/device_list.html',context)

上面my_devices上显示的查询是传递到模板所需的条件

相关问题