简体   繁体   中英

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. 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> . How to send this student.mail to the view 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. 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 }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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