如何处理 Django 嵌套模板标签

how to handle Django nested template tags

如何处理模板标签 {% %} 内的变量,我需要为我的函数提供参数 send_mail_view()

urls.py

urlpatterns = [
path('send_mail_view/<sender_email>/<receiver_email>/<doc>',views.send_mail_view, name='send_mail_view'),

]

mailing_app/views.py

def send_mail_view(request,sender_email,receiver_email,doc):
    email=EmailMessage( 'i want to hire you Nitish ', 'please provide your resume', sender_email, [receiver_email] )
    email.attach_file('mailing_app/179.pdf')
    res=email.send(fail_silently=False)
    success_msg="mail sent success fully"
    return render(request,'mailing_app/success.html',{'success_msg':success_msg})

student_app/views.py

def student_main_view(request,username):
user_obj=User.objects.filter(username=username)
pdf_student_model_obj=pdf_student_model.objects.filter(username__in=user_obj)
pdf_list=[]
for p in pdf_student_model_obj:
    print("nikseva",type(p.pdf_id))
    pdf_indexing_model_obj=pdf_indexing_model.objects.filter(pdf_id=str(p.pdf_id))
    for x in pdf_indexing_model_obj:
        pdf_list.append({'pdf_id':x.pdf_id,'pdf_title':x.pdf_title,'pdf_abstract':x.pdf_abstract,'pdf_path':x.pdf_path,'sender_email':"n@gmail.com",'receiver_email':username,})

print("pdf_list",pdf_list)
return render(request,'rnd_app/student_mainpage.html',{'pdf_list':pdf_list})

templates/mail.html

{% for z in pdf_list %}
  pdf_title :{{z.pdf_title}}
  pdf_abstract:{{z.pdf_abstract}}<br>
  pdf_path:{{z.pdf_path}}
  <a href="{% url 'mailing_app:send_mail_view' {{request.user.username}} {{z.receiver_email}} {{z.pdf_path}} %}" class="btn1 ">Send Email</a>
{% endfor %}

当您已经在模板标签内时,您不需要变量大括号。直接引用即可:

{% url 'mailing_app:send_mail_view' request.user.username z.receiver_email z.pdf_path %}