简体   繁体   English

将变量从模板传递到 django 中的视图

[英]Passing variable from template to view in django

How can I pass value from django templates to views.py??如何将值从 django 模板传递给 views.py? I have written Index.html having view function index() now I have created another view function places() in which I need to pass the value of p tag from Index.html file.我已经编写了具有视图 function index() 的 Index.html,现在我创建了另一个视图 function places(),我需要在其中传递来自 Index.html 文件的 p 标签的值。 It doesn't include any POST or GET method.它不包括任何 POST 或 GET 方法。

Index.html Index.html

{% block content %}
<div >
        {% for key,values in image_data.items  %} 
                <div class="divtag">
                <!-- {{key}} -->
                    <a href="Places" class="tag" >
                        <img src="{{values}}"/>
                        <div class="middle">
                            <p class="center" data={{key}}>{{key}}</p>
                        </div>
                    </a>
                </div>
        {% endfor %}
</div>
{% endblock %}

Views.py视图.py

#main index view
def index(request):
img = MyModel.objects.all()
template = loader.get_template('Index.html')
image_data = {}
for obj in img:

    image_data[obj.image_name] = obj.image

print("-----------------------------------------------------------------")
return render(request,'Index.html',{"image_data" : image_data})

 # where i need to return value from template
def places(request):
name = 'Paris'  #i want name value from p tag in html file
if(name == 'Paris'):

    img = Paris.objects.all()
    template = loader.get_template('Index.html')
    image_data = {
        'images' : [obj.image for obj in img],
        'name' : [obj.image_name for obj in img],
        'description' : [obj.image_description for obj in img]
    }
    image_data_zip = zip([obj.image for obj in img],[obj.image_name for obj in img],[obj.image_description for obj in img])   
    print("-----------------------------------------------------------------")
    return render(request,'Paris.html',{"image_data_zip" : image_data_zip})
else:

    img = Germany.objects.all()
    template = loader.get_template('Index.html')
    image_data = {
        'images' : [obj.image for obj in img],
        'name' : [obj.image_name for obj in img],
        'description' : [obj.image_description for obj in img]
    }
    image_data_zip = zip([obj.image for obj in img],[obj.image_name for obj in img],[obj.image_description for obj in img])   
    print("-----------------------------------------------------------------")
    return render(request,'Germany.html',{"image_data_zip" : image_data_zip})

Sending data from Back-End to Front-End is context in django.将数据从后端发送到前端是 django 中的context

Sending data from Front-End to Back-End is Ajax and it's not a django concept.从前端向后端发送数据是Ajax而不是 django 的概念。

Here's a reference to use ajax with jQuery. Without jQuery, Search for axios 这里参考使用ajax和jQuery。没有jQuery,搜索axios

eg例如

$.ajax({
    url: 'myserver/getID', // no domain needed. careful with CORS (search about it)
    type: 'GET',
    data: // don't specify this, we're not posting any data,
    success: function (response) {console.log(response.data)}, 
    // response will be what returned from python
    error: function (error){console.log(error)}
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM