0

我制作了一个视图,通过模板将电子邮件发送到电子邮件地址。我想发送最初创建请求的用户的名称,但我无法理解它,因为它是 FK。

我尝试通过 for 循环运行它并创建对象,但这只是打印全部数据。

管理员查看 想要的结果

视图.py


def accept_leave(request): #accept email
    all_item = Leave.objects.all()
    context ={'all_item':all_item }

    subject = "Leave Accepted"#email subject
    email_from = "settings.EMAIL_HOST_USER"  # email from
    to_email = ['talhamurtaza@clickmail.info']  # email to

    with open("C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/accept_email.txt", 'rb') as f:
        msgbody = f.read()
    msg = EmailMultiAlternatives(
        subject=subject, body=msgbody, from_email=email_from,to=to_email)
    html_template = get_template(
        "C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/accept_email.html").render()
    msg.attach_alternative(html_template, "text/html")
    msg.send()
    return render(request, 'projectfiles/email.html', context)




def reject_leave(request): #reject email
    all_item = Employee.objects.all()
    context = {'name': request.user}

    subject = "Leave Rejected"  # email subject
    email_from = "settings.EMAIL_HOST_USER"  # email from
    to_email = ['talhamurtaza@clickmail.info']  # email to

    with open(
        "C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/reject_email.txt", 'rb') as f:
        msgbody = f.read()
    msg = EmailMultiAlternatives(
        subject=subject, body=msgbody, from_email=email_from, to=to_email)
    html_template = get_template(
        "C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/reject_email.html").render()
    msg.attach_alternative(html_template, "text/html")
    msg.send()

    return render(request, 'projectfiles/rejectemail.html',context)

模型.py


class Employee(models.Model):

    employee_name = models.OneToOneField(User, on_delete = models.CASCADE)
    employee_designation = models.CharField(max_length = 10)
    employee_department = models.CharField(max_length = 35)


    def __str__(self):
        # return self.employee_department + " " + " "  + str(self.employee_name.username) + " " + " " + " ID: " + str(self.pk)
        return self.employee_name.username

    class Meta:
        verbose_name_plural = "Employee"






class Leave(models.Model):
#This class will connect the user with the leave types etc and other classes data

    employee_leaves = models.ForeignKey(Employee, on_delete=models.CASCADE)
    leave_Type = models.CharField(max_length=25)


    class Meta:
        verbose_name_plural = "Leave"

    def __str__(self):
        return self.leave_Type + "  by  " + str(self.employee_leaves)


电子邮件.html

<title>Email Sent</title>
{% include 'projectfiles/base2.html' %}
<br>
<br>
<br>

<div class='container'>
<center>
<h3>Leave Accepted</h3>
<hr width="50%">
{{request.user}}

<br>
<br>


<h6>
<a href="{% url 'leave_show' %}"> Pending </a><strong> | | </strong><a href="{% url 'Home-Page' %}"> Home </a>
</h6>

</center>
</div>

接受.html

{% include 'projectfiles/base2.html' %}


<h3>Application accepted</h3>
{{request.user}}


<h6>Have a good day</h6>

管理页面.html

<title>Admin Dashboard</title>

{% include 'projectfiles/base2.html' %}

{% block asd %}
{% endblock asd %}
<br><br>

<div class="container">
<center>{% if count %}

<h3>Pending: <font color='#1783FF'> "{{ count }}"</font></h3><hr width="40%" noshade>
{% else %}
<br><br> <h2>NO LEAVES</h2>
{% endif %}</center>
</div>
<br>




{% for obj in all_items%}
<center>
<div class="container">
<h3><b>Applicant:</b> {{obj.employee_leaves}}</h3>
<b>Leave Type:</b> {{obj.leave_Type}}<br>
<small>Leave ID: {{obj.id}}</small><br>
<a href="{% url 'Accept-page' %}"> Accept</a> <b>||</b> <a href="{% url 'Reject-page' %}">Reject</a>
<hr width="50%"></center>
</div>


{% endfor %}

网址.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views


urlpatterns = [

    url(r'^(?i)Home/$', views.home, name='Home-Page'),
    url(r'^(?i)request/', views.request_leave, name='Leave-Page'),
    url(r'^(?i)user_dlt/', views.userdlt_page, name='userdlt-Page'),
    url(r'^password/$', views.change_password, name='change_password'),




    # ADMIN DASHBOARD PAGES
    url(r'^(?i)showprofile/$', views.showusers, name='Users-Page'),
    url(r'^(?i)leaves/$', views.leave_show, name='leave_show'),
    url(r'^acceptemail/$', views.accept_leave, name='Accept-page'),
    url(r'^rejectemail/$', views.reject_leave, name='Reject-page'),

    #==================




    #=========login,logout & User registration URLS=========
    url(r'^(?i)login/$', auth_views.LoginView.as_view(template_name='projectfiles/login.html'), name='LoginPage'),
    url(r'^(?i)logout/$', auth_views.LogoutView.as_view(template_name='projectfiles/logout.html'), name='LogoutPage'),
    url(r'^(?i)registraion/$', views.reg_user, name='RegPage'),
    url(r'^(?i)notifications/$', views.notifications, name='notifications-Page'),
    #=========End=========


]

错误

[22/May/2019 10:21:39] "GET /favicon.ico HTTP/1.1" 404 4170
[22/May/2019 10:21:46] "GET /admin HTTP/1.1" 301 0
[22/May/2019 10:21:46] "GET /admin/ HTTP/1.1" 200 6253
[22/May/2019 10:21:47] "GET /static/admin/css/base.css HTTP/1.1" 200 16066
[22/May/2019 10:21:47] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[22/May/2019 10:21:47] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
[22/May/2019 10:21:47] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
[22/May/2019 10:21:47] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
[22/May/2019 10:21:47] "GET /static/admin/img/icon-deletelink.svg HTTP/1.1" 200 392
[22/May/2019 10:21:47] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
[22/May/2019 10:21:47] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[22/May/2019 10:21:47] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[22/May/2019 10:21:52] "GET /home/ HTTP/1.1" 200 7492
[22/May/2019 10:21:52] "GET /static/css/styles.css HTTP/1.1" 200 304
[22/May/2019 10:21:52] "GET /static/js/file.js HTTP/1.1" 200 150
[22/May/2019 10:21:58] "GET /leaves/ HTTP/1.1" 200 2613
[22/May/2019 10:21:58] "GET /static/js/login.js HTTP/1.1" 200 293
[22/May/2019 10:22:05] "GET /acceptemail/4/ HTTP/1.1" 302 0
Not Found: /acceptemail/4/




<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
    integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="/static/css/styles.css">





    <style>
      body {
  background-color:
[22/May/2019 10:22:05] "GET /acceptemail/4/%0A%0A%0A%0A%0A%3C!doctype%20html%3E%0A%3Chtml%20lang=%22en%22%3E%0A%0A%3Chead%3E%0A%20%20%3C!--%20Required%20meta%20tags%20--%3E%0A%20%20%3Cmeta%20charset=%22utf-8%22%3E%0A%20%20%3Cmeta%20name=%22viewport%22%20content=%22width=device-width,%20initial-scale=1,%20shrink-to-fit=no%22%3E%0A%0A%20%20%3C!--%20Bootstrap%20CSS%20--%3E%0A%20%20%3Clink%20rel=%22stylesheet%22%20href=%22https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css%22%0A%20%20%20%20integrity=%22sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm%22%20crossorigin=%22anonymous%22%3E%0A%20%20%20%20%3Clink%20rel=%22stylesheet%22%20href=%22/static/css/styles.css%22%3E%0A%0A%0A%0A%0A%0A%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%20%20body%20%7B%0A%20%20background-color: HTTP/1.1" 404 6369

新观点

def accept_leave(request, id):  # accept email
    all_item = Leave.objects.get(id=id)
    context ={'all_item': all_item}

    subject = "Leave Accepted"#email subject
    email_from = "settings.EMAIL_HOST_USER"  # email from
    to_email = ['talhamurtaza@clickmail.info']  # email to

    with open("C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/accept_email.txt", 'rb') as f:
        msgbody = f.read()
    msg = EmailMultiAlternatives(
        subject=subject, body=msgbody, from_email=email_from,to=to_email)
    html_template = get_template(
        "C:/Users/Bitswits 3/Desktop/Intern Work/LMS/LMS/projectfiles/templates/projectfiles/email/accept_email.html").render()
    msg.attach_alternative(html_template, "text/html")
    msg.send()

    html = render_to_string(
        "projectfiles/email/accept_email.html", {'all_item': all_item})

    return HttpResponseRedirect(html)

我想要请求用户的姓名和其他一些信息,例如他选择通过电子邮件模板传递并发送的休假类型。如果我希望一个对象从其挂起状态移动到完成状态,如管理页面所示,您能否指导我采取什么方法。

在此处输入图像描述

4

1 回答 1

0

你的代码有很多问题:

  1. 你的观点accept_leave没有收到申请人的名字。您的“接受”按钮不应是指向accept_leave(GET 请求)的链接,但您应该为每个申请人创建一个表单,并且“接受”按钮应提交带有申请人 ID 的表单(作为 POST 请求)(或用户名,唯一的东西)在 URL 中或作为输入字段。在这里查看以了解如何使用表单。

    或者,如果您真的想使用 GET 而不是使用表单,请将accept_leave视图的 URL 更改为也具有申请人 id ( /acceptemail/(?P<id>[0-9]+)$) 并将 id 传递给模板 ( {% url 'accept-page' id=obj.id %})中的 url 构造函数

  2. 将申请人 ID 传递到视图后,您将使用该对象检索它,employee = Employee.objects.get(id=...)并且可以使用该对象填写发送电子邮件所需的内容(还可以Leave为该员工创建对象)。

  3. 您想使用 Django 的模板渲染引擎来渲染您的模板,因此您可以将上下文变量传递给您的模板(例如employee)并在您的电子邮件模板中使用它们(例如{{ employee.name }})。您可以使用 来做到这一点render_to_string,例如:

    html = render_to_string("projectfiles/email/accept_email.html", {'employee': employee})
    text = render_to_string("projectfiles/email/accept_email.txt", {'employee': employee})
    

    然后使用htmltext发送电子邮件。

于 2019-05-21T10:57:51.627 回答