我已经建立了一个视图,该视图通过模板将电子邮件发送到电子邮件地址。我想发送最初创建请求的用户名,但由于是FK,所以无法解决。
我已经尝试过通过for循环运行它并创建对象,但这只会打印出全部数据。
Views.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)
models.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)
email.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>
accept.html
{% include 'projectfiles/base2.html' %}
<h3>Application accepted</h3>
{{request.user}}
<h6>Have a good day</h6>
adminpage.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 %}
urls.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)
我希望发出请求的用户名和一些其他信息,例如他选择传递电子邮件模板并发送的休假类型。 如果我希望将一个对象从挂起状态转变为已完成状态,请问我该采取什么方法,如管理页面所示。
答案 0 :(得分:0)
您的代码有很多问题:
您的视图accept_leave
未收到申请人的姓名。您的“接受”按钮不应链接到accept_leave
(GET请求),但您应为每个申请人创建一个表单,并且“接受”按钮应提交表单(作为POST请求)在网址中或输入字段中包含申请人的ID(或用户名,唯一的名称)。选中here,了解如何使用表单。
或者,如果您确实要使用GET而不是使用表单,请更改您的accept_leave
视图的URL以也具有申请人ID(/acceptemail/(?P<id>[0-9]+)$
)并将ID传递给您的URL模板({% url 'accept-page' id=obj.id %}
)中的构造函数
将申请人ID传递到视图后,您将使用employee = Employee.objects.get(id=...)
检索它,并且可以使用该对象填写发送电子邮件所需的内容(也可以创建电子邮件该员工的Leave
对象)。
您想使用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})
然后使用html
和text
发送电子邮件。