我有一个应用程序,其中主用户添加了一个子用户并向他发送电子邮件。我希望该子用户在电子邮件模板中具有一个按钮,该按钮会将他/她引导到“反馈”表单。该反馈表单的url必须带有用户名,因为它具有个人风格。但是我不明白如何传递子用户数据并根据数据保存反馈数据。我将创建一个方案,以使大家更好地理解。
1。。有一个主要用户Chris ,克里斯在其电子邮件列表中添加了一个子用户Leon 。 (PS均为常驻邪恶角色)。 2。。克里斯发送电子邮件给莱昂,莱昂将收到该电子邮件,如果他希望他可以单击电子邮件中的“反馈”按钮来提供反馈。 3。。如果Leon点击该按钮,他将被重定向到该URL可能为(www.feedback/Leon.com)之类的页面,将显示以下表格Leon将在其中添加评论并提交。
问题是我如何根据来自子用户模型(PersonData)的信息通过电子邮件表单将Leon的反馈保存在FeedBack模型中。
class FeedBack(models.Model):
feedback = models.TextField('Feedback')
user_feedback = models.ForeignKey(PersonData)
class PersonData(models.Model):
user_relation = models.ForeignKey(User)
person_first_name = models.CharField("First Name", max_length=25)
person_last_name = models.CharField("last Name", max_length=25)
person_email = models.EmailField("Email", max_length=512)
# class FeedBackCreateView(CreateView):
# model = FeedBack
# template_name = "userfiles/FeedBack.html"
# def form_valid(self, form):
# obj = form.save(commit=False)
# obj.user_feedback_id = self.request.persondata_id
# obj.save()
# return redirect("Home-Page")
def feedback(request, username):
person_data = PersonData.objects.get(person_first_name=username)
item = FeedBack.objects.all().filter(user_feedback=person_first_name)
form = FeedBackForm
if request.method == 'POST':
if form.is_valid():
form.save(commit=False)
profile.employee_name = user
profile.save()
return redirect('Home-Page')
context = {'all_item': all_item}
return render(request, 'userfiles/AlbumPicsList.html', context)
------------------------------------------------------------------
#This View is from another app called salami. This sends the email
------------------------------------------------------------------
def send_email(request, s_id, pr_id):
salami_data = SalamiCalculator.objects.get(id=s_id)
person_data = PersonData.objects.get(id=pr_id)
context = {'salami_data': salami_data,
'person_data': person_data}
subject = "Thank You"
email_from = settings.EMAIL_HOST_USER # email from
to_email = [person_data.person_email] # email to
msg = EmailMultiAlternatives()
msg.from_email = email_from
msg.to = to_email
msg.subject = subject
msg.body = salami_data.thankyou_msg
html_body = render_to_string("salamifiles\email-thanks.html")
html_template = get_template("salamifiles\email-thanks.html")
html_content = html_template.render(context)
msg.attach_alternative(html_content, "text/html")
msg.send()
messages.success(request, ("Email Sent !!"))
return render(request, 'salamifiles\email-thanks.html', context)
url(r'^(?i)Feedback/(?P<user_feedback>\w+)/$',
AlbumPicturesDetail.as_view(), name='Album-Pictures-View'),
==============================================
Salami URL
==============================================
url(r'^Congratsmail/(?P<s_id>[0-9]+)/(?P<pr_id>\d+)/$',
views.send_email, name='Congrats-EMAIL'),
class FeedBackForm(forms.ModelForm):
class Meta:
model = FeedBack
fields = ("feedback")
Have Not Yet modified the email template by adding the button
{% for i in items %}
<a href={% url 'Congrats-EMAIL' s_id=i.id pr_id=i.realted_person.id %}>Send Mail</a>
{% endfor %}
============================================= ===========================
我希望当克里斯向莱昂发送电子邮件时,莱昂通过单击按钮会被重定向到该页面,该页面具有URL的名称,并且该表单已经知道他是莱昂,以便将数据保存在其ID /名称中。 / p>
答案 0 :(得分:1)
我不明白你为什么不能做
class FeedBack(models.Model):
feedback = models.TextField('Feedback')
user_feedback = models.ForeignKey(PersonData, related_name="received_feedbacks")
giver_feedback = models.ForeignKet(PersonData, related_name="given_feedbacks")
答案 1 :(得分:0)
我同意@HenryM,您可以添加一个用于与PersonData相关的额外字段。
class FeedBack(models.Model):
feedback = models.TextField('Feedback')
user = models.ForeignKey(PersonData, related_name="received_feedbacks")
giver = models.ForeignKet(PersonData, related_name="given_feedbacks")
然后,您可以像这样使用它:
from django.contrib.auth.decorators import login_required
@login_required
def feedback(request, username):
person_data = PersonData.objects.get(person_first_name=username)
item = FeedBack.objects.all().filter(user_feedback=person_first_name)
form = FeedBackForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
feedback = form.save(commit=False)
feedback.user = person_data
feedback.giver = PersonData.objects.get(user_relation=request.user)
feedback.save()
profile.employee_name = user
profile.save()
return redirect('Home-Page')
context = {'form': form}
return render(request, 'form_which_renders_feedbackform.html', context)
我建议对PersonData中的user_relation
字段使用OneToOne关系而不是ForeignKey。这样您就可以简单地通过request.user.persondata
访问它。