简体   繁体   English

如何将变量从一个视图发送到另一个视图?

[英]How to send variables from one views to another?

in my html template I have a variable {{ student.mail}} from context in view Nr.1.在我的 html 模板中,我在视图 Nr.1 中有一个来自上下文的变量{{ student.mail}} In this template I also have button <a class="btn btn-warning" href="{% url 'send_and_home' mail=student.mail %}" role="button">Save and Send</a> .在这个模板中,我还有按钮<a class="btn btn-warning" href="{% url 'send_and_home' mail=student.mail %}" role="button">Save and Send</a> How to send this student.mail to the view Nr.2.如何将这个 student.mail 发送到视图 Nr.2。 The first view returns home page without any.第一个视图返回没有任何主页。 My second page also returns the same home page, but also inside have an additional function to send mail.我的第二页也返回同样的主页,而且里面还有一个额外的function发送邮件。 I don't understand how to implement something like this?我不明白如何实施这样的事情? Could you help me?你可以帮帮我吗?

my urls:我的网址:

path('home/', views.home, name="home"),

my 1st view:我的第一个观点:

   @login_required(login_url='login')
    def home(request):
        # smth
    
        context = ...
        return render(request, 'app/home.html', context)

my 2nd view:我的第二个观点:

@login_required(login_url='login')
def send_and_home(request, mail):
    # article, text, to for mail() depends on mail var from my template
    mail()
    

    context = the same as a view Nr.1
    return render(request, 'app/home.html', context)


def mail(request, article, text, to):
    
    return send_mail(
    article,
    'text',
    '........',
    [to,],
    fail_silently=False,
)

Ok, I am just add new tag:好的,我只是添加新标签:

import re
from django import template
from django.core.mail import send_mail
register = template.Library()

@register.filter
def mail(address):
    return send_mail(
    '....',
    'Test 2) ',
    '.....',
    [address,],
    fail_silently=False,
    )

And then in template:然后在模板中:

{% load mail %}
....
{{ var | mail }}

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

相关问题 如何将视图从一个django views.py导入到另一个 - How to import views from one django views.py to another 如何在第一个脚本运行时将 argparse 变量从一个脚本发送到另一个脚本? - How do I send argparse variables from one script to another while the first script is running? 如何从一种方法访问变量到另一种方法? - How to access variables from one method into another? 如何将值/变量从一个 function 传递到 django 中的另一个 - 视图 - How to pass value/variable from one function to another in django - views 如何在另一个班级中使用一个班级的变量 - How to use variables from one class in another class 如何将查询变量从一个HTML页面传送到另一个HTML页面? - How to carry query variables from one html page to another? 如何将变量从一个函数传递到另一个函数? - How do I get my variables from one function to another? 如何将特定变量从一个类继承到另一个类? - How to inherit specific variables from one class to another class? 如何将javascript变量/对象从一个文件复制到另一个文件 - How to copy javascript variables/objects from one file to another file 如何将局部变量从一个函数传递到另一个函数? - How to pass local variables from one function into another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM