如何处理Django嵌套模板标签

时间:2019-02-28 09:14:40

标签: python django django-email

如何处理模板标记{%%}中的变量,我需要给函数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 %}

1 个答案:

答案 0 :(得分:2)

当您已经在模板标签中时,不需要大括号。只需直接引用它们即可:

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