我有一个模型表单,用于保存更改请求,并在创建新条目时通过电子邮件通知管理员。管理员获取并通过链接进行温和通知,以查看条目http://mydjango/admin,然后他们登录查看并批准或拒绝请求。管理员希望上面的链接将它们直接带到条目而不是管理站点,让他们浏览条目列表以找到最新的条目。 我不得不说,我是django的新手,并希望看看这是否可行。我注意到,如果我转到某个条目并单击它,我的浏览器上的链接就会有一个唯一的编号,例如:http://127.0.0.1:8000/admin/app/changerequests/61/change/
每个条目都有61个更改。所以我想的可能包括那个" id"发送给管理员的电子邮件中新创建的条目。
她是我的模特,形式,观点,管理员
model.py:
from __future__ import unicode_literals
from django.utils import timezone
from django.db import models
from django.core.mail import send_mail
from django.conf import settings
from app.forms import SignUpForm
# Create your models here.
class ChangeRequests(models.Model):
ENV_CHOICE = (
('select', 'Select'),
('Production', 'Production'),
('Lab', 'Lab'),
('EBC_env', 'EBC_env'),
('Los_Angles', 'Los Angles'),
)
request_status = (
('select', 'Select'),
('Pending...', 'Pending...'),
('Approved', 'Approved'),
('Denied', 'Denied'),
('Added_comment', 'Added_comment'),
)
email = models.EmailField()
full_name = models.CharField(max_length=120, blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
what_is_the_change = models.TextField(max_length=250, null=True)
impact = models.TextField(max_length=250, null=True, help_text="Provide an example of the impact this change might cause")
environment = models.CharField(max_length=10, choices=ENV_CHOICE, default='select')
change_date_and_time = models.DateTimeField(default=timezone.now, verbose_name="Date/time of change. Use format: 3/30/2016 20:30")#, help_text="time example: <em>03/30/2016 20:30</em>")
#test_time = models.CharField(widget=SelectDateWidget, initial=datetime.date.today())
status = models.CharField(max_length=20, choices=request_status, default='Pending...', blank=True)
status_reason = models.CharField(max_length=300, blank=True, null=True)
#this code is for checking the status of the request and sends an email with a message based on the comment left by the admins:
__original_status = None
def __init__(self, *args, **kwargs):
super(ChangeRequests, self).__init__(*args, **kwargs)
self.__original_status = self.status
def save(self, force_insert=False, force_update=False, *args, **kwargs):
if self.status != self.__original_status:
subject = "Request for a change status changed"
from_email = settings.EMAIL_HOST_USER + "@company.com"
if self.status_reason != "":
contact_message = """
Hi: %s
Your request to make a change to: %s
Has been: (%s)
wiht the following comment: (%s)
"""%(self.full_name, self.environment, self.status, self.status_reason)
else:
contact_message = """
---------
- Hi: %s
-
Your request to make a change to: %s
Has been: (%s)
"""%(self.full_name, self.environment, self.status)
to_email = (self.email,)
send_mail(subject, contact_message, from_email, to_email, fail_silently=False)
super(ChangeRequests, self).save(force_insert, force_update, *args, **kwargs)
self.__original_status = self.status
def __unicode__(self):
return self.email
forms.py:
from django import forms
from app.models import ChangeRequests
from django.utils import timezone
import datetime
class SignUpForm(forms.ModelForm):
class Meta:
model = ChangeRequests
fields = ('full_name', 'email', 'environment', 'what_is_the_change', 'change_date_and_time','impact')
def clean_email(self):
email = self.cleaned_data.get('email')
return email
def clean_full_name(self):
full_name = self.cleaned_data.get('full_name')
return full_name
views.py:
from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings
from app.forms import SignUpForm, ContactForm
from app.models import ChangeRequests
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect, HttpResponse
# Create your views here.
@csrf_exempt
def home(request):
title = "WELCOME GUEST"
form = SignUpForm(request.POST)
context = {
"title": title,
"form": form
}
if form.is_valid():#this cases the form to be saved into the DB I can also have it where if form.is_valid(): form.save()
form.save()
form_email = form.cleaned_data.get("email")
form_message = form.cleaned_data.get("message")
form_full_name = form.cleaned_data.get("full_name")
form_environment = form.cleaned_data.get("environment")
form_what_is_the_change = form.cleaned_data.get("what_is_the_change")
form_change_date_and_time = form.cleaned_data.get("change_date_and_time")
#form_status = form.cleaned_data.get("status")
subject = "Request for a change"
from_email = settings.EMAIL_HOST_USER + "@company.com"
to_email = ["test@company.com"]
contact_message = """
User: %s
Email: %s
Requesting the following change: (%s)
Environment affected: (%s)
Date and time of the change: %s
Admin approvals and review >>>>> http://myserver/admin and use your admin login credentials.
"""%(form_full_name, form_email, form_what_is_the_change, form_environment, form_change_date_and_time)
send_mail(subject, contact_message, from_email, to_email, fail_silently=False)
return HttpResponseRedirect('/submitted')
context = {
"title": "Thank you"
}
return render(request, 'home.html', context)
答案 0 :(得分:0)
我无法看到您在该电子邮件中包含任何类型的链接的位置,但您当然可以使用reversing admin urls功能创建指向您的内容的链接。
from django.core.urlresolvers import reverse
...
if form.is_valid():
item = form.save()
link = reverse('admin:app_changerequests_change', args=(item.id,))
url = "https://ADDRESS_OF_MY_SITE{}".format(link)
您可以在电子邮件中加入url
值。
答案 1 :(得分:0)
就像你的模型一样,它是一样的。所以,你可以传递id或pk。
据您所知,Django始终在每个模型中添加id
字段以获得唯一性,pk
表示您的主键字段。因此,您可以将任何字段分配给主键(默认情况下,它分配给id
字段)。所以,您曾经为任何一个字段指定了主键。然后,当你致电pk
时。它将返回主键字段的值。
希望它会对你有所帮助。 :)
答案 2 :(得分:0)
您可以使用
获取模型对象的正确管理员更改网址reverse('admin:app_model_change', args=(model_object.id))
在您的电子邮件中发送网址。