如何从Django模板中调用特定的python函数

时间:2023-01-24 11:24:11

I have a button to copy data from a user uploaded file to the clipboard in a specific format. I already have that data saved in the database as it was uploaded in a separate file form. I currently have it so that upon clicking the copy to clipboard button it is linked to a copy_data view in my views.py that requires an HTTP request which redirects to the current template containing the copy to clipboard button with something like this:

我有一个按钮,用于将用户上传文件中的数据以特定格式复制到剪贴板。我已经将数据保存在数据库中,因为它是以单独的文件形式上传的。我目前拥有它,以便在单击复制到剪贴板按钮时,它链接到我的views.py中的copy_data视图,该视图需要HTTP请求,该请求重定向到包含复制到剪贴板按钮的当前模板,如下所示:

HttpResponseRedirect('previous/template/here')

HttpResponseRedirect( '一/模板/这里')

This works fine except for the fact that since it links to my copy_data view which then redirects to the original view containing the button it reloads the entire page which is undesirable.

这很好用,因为它链接到我的copy_data视图,然后重定向到包含按钮的原始视图,它重新加载整个页面,这是不可取的。

I think a better solution would be to somehow bind a python function directly to the button click rather than worrying about redirecting from one view to another.

我认为更好的解决方案是以某种方式将python函数直接绑定到按钮单击,而不是担心从一个视图重定向到另一个视图。

I've found many examples using ajax, but haven't found any that work for my use case. I tried binding a click event to the button without any problems, but I am stuck on figuring out how to bind the python function with the click.

我发现很多使用ajax的例子,但是没有找到任何适用于我的用例。我尝试将单击事件绑定到按钮没有任何问题,但我仍然坚持要弄清楚如何将python函数与click结合。

How can I bind a python function in my Django template upon a button press?

如何在按下按钮的情况下在我的Django模板中绑定python函数?

1 个解决方案

#1


1  

It's tough to tell for sure, but I think you're mixing synch/asynch paradigms here. When you generate requests with Ajax, you don't (generally) want to return a redirect, you want to return data. That might be JSON data or data formatted as a specific MIME type or even just text. One way this might look at a high level is:

很难说清楚,但我认为你在这里混合使用synch / asynch范例。当您使用Ajax生成请求时,您(通常)不希望返回重定向,而是希望返回数据。这可能是JSON数据或格式化为特定MIME类型的数据,甚至只是文本。这可能是高层次的一种方式:

def copy_data(request):
    # get posted data
    submitted = request.POST
    # do whatever is necessary to create document
    data = ???
    # first, we'll need a response
    resp = HttpResponse() 
    # set the content type, if needed
    resp.content_type = 'text/???; charset=utf-8'
    # response has a file-like interface
    resp.write(data)
    return resp

Obviously, this would need work to suit your purpose, but that's the high-level approach.

显然,这需要适合您的目的,但这是高级方法。

It doesn't sound like you're returning JSON, but there's a special response object for that now if you need it.

这听起来并不像你正在返回JSON,但如果你需要它,现在有一个特殊的响应对象。

#1


1  

It's tough to tell for sure, but I think you're mixing synch/asynch paradigms here. When you generate requests with Ajax, you don't (generally) want to return a redirect, you want to return data. That might be JSON data or data formatted as a specific MIME type or even just text. One way this might look at a high level is:

很难说清楚,但我认为你在这里混合使用synch / asynch范例。当您使用Ajax生成请求时,您(通常)不希望返回重定向,而是希望返回数据。这可能是JSON数据或格式化为特定MIME类型的数据,甚至只是文本。这可能是高层次的一种方式:

def copy_data(request):
    # get posted data
    submitted = request.POST
    # do whatever is necessary to create document
    data = ???
    # first, we'll need a response
    resp = HttpResponse() 
    # set the content type, if needed
    resp.content_type = 'text/???; charset=utf-8'
    # response has a file-like interface
    resp.write(data)
    return resp

Obviously, this would need work to suit your purpose, but that's the high-level approach.

显然,这需要适合您的目的,但这是高级方法。

It doesn't sound like you're returning JSON, but there's a special response object for that now if you need it.

这听起来并不像你正在返回JSON,但如果你需要它,现在有一个特殊的响应对象。