电子邮件字段中的 django UniqueConstraint 错误(发送网格验证电子邮件)
django UniqueConstraint error in email field (sendgrid verification email)
我正在尝试对我的 django 项目实施 sendgrid 电子邮件验证。
我一直在关注 https://www.twilio.com/blog/send-dynamic-emails-python-twilio-sendgrid 教程。我有相应的模型和视图设置,但是,当我尝试两次使用相同的电子邮件地址时遇到 UniqueConstraint 错误(尽管我的模型将电子邮件字段设置为 unique = True)。我不想让 UniqueConstraint 错误,而是希望该应用程序只是为了防止用户两次添加相同的电子邮件。我尝试使用 cleaned_data 作为表单,但无论如何我都无法解决这个 UniqueConstraint 错误。
我的错误:
异常类型:IntegrityError
异常值:
UNIQUE 约束失败:newsletter_newsletteruser.email
我的models.py:
from django.db import models
class NewsletterUser (models.Model):
email= models.EmailField(unique=True)
date_added = models.DateTimeField(auto_now_add=True)
conf_num = models.CharField(max_length=15)
confirmed = models.BooleanField(default=False)
def __str__(self):
return self.email + " (" + ("not " if not self.confirmed else "") + "confirmed)"
我的admin.py
from .models import NewsletterUser, MailMessage
# Register your models here.
class NewsletterAdmin(admin.ModelAdmin):
list_display = ('email','date_added','conf_num', 'confirmed')
admin.site.register(NewsletterUser, NewsletterAdmin)
admin.site.register(MailMessage)
我的views.py
from django.db.models.fields import EmailField
from django.shortcuts import render, redirect
from sendgrid.helpers.mail.bcc_email import Bcc
from django.http import HttpResponse
from .forms import NewsletterUserForm, MailMessageForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, Content, To
from .models import NewsletterUser
from django import forms
import pandas as pd
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
import random
from sendgrid import SendGridAPIClient
# Create your views here.
#!REGISTER CURRENT HOST!
host = 'http://127.0.0.1:8000/'
# Helper Functions
def random_digits():
return "%0.12d" % random.randint(0, 999999999999)
@csrf_exempt
def newsletters(request):
if request.method == 'POST':
sub = NewsletterUser(email=request.POST['email'], conf_num=random_digits())
sub.save()
message = Mail(
from_email='pasiekaradosc@gmail.com',
to_emails=sub.email,
subject='Potwierdzenie Adresu Email w Newsletterze Pasieka Radość',
html_content= "<h1>Dziękujemy za zapisanie się do newslettera Pasieka Radość!<h1> \
<h2>Aby potwierdzić rejestrację newslettera</h2> \
<h2><a href='{}confirm/?email={}&conf_num={}'> Kliknij w poniższy link</a>.<h2>".format(request.build_absolute_uri(host),
sub.email,
sub.conf_num))
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
return render(request, 'newsletter/newsletter.html', {'email': sub.email, 'action': 'wysłano', 'form': NewsletterUserForm()})
else:
return render(request, 'newsletter/newsletter.html', {'form': NewsletterUserForm()})
def confirm(request):
sub = NewsletterUser.objects.get(email=request.GET['email'])
if sub.conf_num == request.GET['conf_num']:
sub.confirmed = True
sub.save()
messages.success(request, f'Dziękujemy za zapisanie adresu email do naszego newslettera!')
return render(request, 'pages/home.html' , {'email': sub.email, 'action': 'confirmed'})
我的forms.py
from django import forms
from .models import NewsletterUser, MailMessage
class NewsletterUserForm (forms.ModelForm):
email = forms.EmailField(label='Twój email',
max_length=100,
widget=forms.EmailInput(attrs={'class': 'form-control'}))
class Meta:
model = NewsletterUser
fields = ['email']
我的 html 模板 (newsletter.html)
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content%}
<h1> {{title}} </h1>
<h4>Zapisz się do newslettera i odbieraj najświeższe informacje dotyczące naszej pasieki i oferty.</h4>
<div class="col-12">
{% if email %}
<p class="alert alert-info">Na {{ email }} {{ action }} wiadomość weryfikacyjną. Otwórz wiadomość i kliknij w link aby dodać adres do newslettera. </p>
{% endif %}
<form method="POST" autocomplete="off">
{% csrf_token %}
<div class="col-sm-4">
{{form|crispy}}
</div>
<br>
<div class="col-sm-4">
<input class="btn btn-info" type='submit' value='Zapisz Się'>
</div>
</form>
<br>
<div class="row">
<div class="col-sm-4">
<p>Spokojnie, nie będziemy wysyłać Ci tony niepotrzebnych wiadomości. Zapisz się do newslettera i dowiedz się pierwszy/pierwsza, kiedy w naszej pasiecie pojawi się nowa dostawa miodów lub innych interesujących produktów.</p>
</div>
</div>
</div>
{% endblock content %}
我对 django 和 sendgrid 还很陌生,所以我想知道我是否需要以某种方式完全重建我的代码,或者我可以采取一些步骤让它像上面那样工作。如果有任何提示,请分享。谢谢
此处为 Twilio SendGrid 开发人员布道师。
我相信 Django 在这里的表现符合预期。来自 the Django documentation:
If you try to save a model with a duplicate value in a unique field, a django.db.IntegrityError
will be raised by the model’s save()
method.
您需要做的是捕获那个 IntegrityError
,然后将错误返回给用户。
@csrf_exempt
def newsletters(request):
if request.method == 'POST':
sub = NewsletterUser(email=request.POST['email'], conf_num=random_digits())
try:
sub.save()
message = Mail(
from_email='pasiekaradosc@gmail.com',
to_emails=sub.email,
subject='Potwierdzenie Adresu Email w Newsletterze Pasieka Radość',
html_content= "<h1>Dziękujemy za zapisanie się do newslettera Pasieka Radość!<h1> \
<h2>Aby potwierdzić rejestrację newslettera</h2> \
<h2><a href='{}confirm/?email={}&conf_num={}'> Kliknij w poniższy link</a>.<h2>".format(request.build_absolute_uri(host),
sub.email,
sub.conf_num))
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
return render(request, 'newsletter/newsletter.html', {'email': sub.email, 'action': 'wysłano', 'form': NewsletterUserForm()})
except IntegrityError as ex:
# Return the error to your user
else:
return render(request, 'newsletter/newsletter.html', {'form': NewsletterUserForm()})
我已经在上面的代码中添加了 try/except
,当电子邮件不唯一时,您需要在 except
块中填写要执行的操作。
我正在尝试对我的 django 项目实施 sendgrid 电子邮件验证。 我一直在关注 https://www.twilio.com/blog/send-dynamic-emails-python-twilio-sendgrid 教程。我有相应的模型和视图设置,但是,当我尝试两次使用相同的电子邮件地址时遇到 UniqueConstraint 错误(尽管我的模型将电子邮件字段设置为 unique = True)。我不想让 UniqueConstraint 错误,而是希望该应用程序只是为了防止用户两次添加相同的电子邮件。我尝试使用 cleaned_data 作为表单,但无论如何我都无法解决这个 UniqueConstraint 错误。
我的错误:
异常类型:IntegrityError
异常值:
UNIQUE 约束失败:newsletter_newsletteruser.email
我的models.py:
from django.db import models
class NewsletterUser (models.Model):
email= models.EmailField(unique=True)
date_added = models.DateTimeField(auto_now_add=True)
conf_num = models.CharField(max_length=15)
confirmed = models.BooleanField(default=False)
def __str__(self):
return self.email + " (" + ("not " if not self.confirmed else "") + "confirmed)"
我的admin.py
from .models import NewsletterUser, MailMessage
# Register your models here.
class NewsletterAdmin(admin.ModelAdmin):
list_display = ('email','date_added','conf_num', 'confirmed')
admin.site.register(NewsletterUser, NewsletterAdmin)
admin.site.register(MailMessage)
我的views.py
from django.db.models.fields import EmailField
from django.shortcuts import render, redirect
from sendgrid.helpers.mail.bcc_email import Bcc
from django.http import HttpResponse
from .forms import NewsletterUserForm, MailMessageForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, Content, To
from .models import NewsletterUser
from django import forms
import pandas as pd
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
import random
from sendgrid import SendGridAPIClient
# Create your views here.
#!REGISTER CURRENT HOST!
host = 'http://127.0.0.1:8000/'
# Helper Functions
def random_digits():
return "%0.12d" % random.randint(0, 999999999999)
@csrf_exempt
def newsletters(request):
if request.method == 'POST':
sub = NewsletterUser(email=request.POST['email'], conf_num=random_digits())
sub.save()
message = Mail(
from_email='pasiekaradosc@gmail.com',
to_emails=sub.email,
subject='Potwierdzenie Adresu Email w Newsletterze Pasieka Radość',
html_content= "<h1>Dziękujemy za zapisanie się do newslettera Pasieka Radość!<h1> \
<h2>Aby potwierdzić rejestrację newslettera</h2> \
<h2><a href='{}confirm/?email={}&conf_num={}'> Kliknij w poniższy link</a>.<h2>".format(request.build_absolute_uri(host),
sub.email,
sub.conf_num))
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
return render(request, 'newsletter/newsletter.html', {'email': sub.email, 'action': 'wysłano', 'form': NewsletterUserForm()})
else:
return render(request, 'newsletter/newsletter.html', {'form': NewsletterUserForm()})
def confirm(request):
sub = NewsletterUser.objects.get(email=request.GET['email'])
if sub.conf_num == request.GET['conf_num']:
sub.confirmed = True
sub.save()
messages.success(request, f'Dziękujemy za zapisanie adresu email do naszego newslettera!')
return render(request, 'pages/home.html' , {'email': sub.email, 'action': 'confirmed'})
我的forms.py
from django import forms
from .models import NewsletterUser, MailMessage
class NewsletterUserForm (forms.ModelForm):
email = forms.EmailField(label='Twój email',
max_length=100,
widget=forms.EmailInput(attrs={'class': 'form-control'}))
class Meta:
model = NewsletterUser
fields = ['email']
我的 html 模板 (newsletter.html)
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content%}
<h1> {{title}} </h1>
<h4>Zapisz się do newslettera i odbieraj najświeższe informacje dotyczące naszej pasieki i oferty.</h4>
<div class="col-12">
{% if email %}
<p class="alert alert-info">Na {{ email }} {{ action }} wiadomość weryfikacyjną. Otwórz wiadomość i kliknij w link aby dodać adres do newslettera. </p>
{% endif %}
<form method="POST" autocomplete="off">
{% csrf_token %}
<div class="col-sm-4">
{{form|crispy}}
</div>
<br>
<div class="col-sm-4">
<input class="btn btn-info" type='submit' value='Zapisz Się'>
</div>
</form>
<br>
<div class="row">
<div class="col-sm-4">
<p>Spokojnie, nie będziemy wysyłać Ci tony niepotrzebnych wiadomości. Zapisz się do newslettera i dowiedz się pierwszy/pierwsza, kiedy w naszej pasiecie pojawi się nowa dostawa miodów lub innych interesujących produktów.</p>
</div>
</div>
</div>
{% endblock content %}
我对 django 和 sendgrid 还很陌生,所以我想知道我是否需要以某种方式完全重建我的代码,或者我可以采取一些步骤让它像上面那样工作。如果有任何提示,请分享。谢谢
此处为 Twilio SendGrid 开发人员布道师。
我相信 Django 在这里的表现符合预期。来自 the Django documentation:
If you try to save a model with a duplicate value in a unique field, a
django.db.IntegrityError
will be raised by the model’ssave()
method.
您需要做的是捕获那个 IntegrityError
,然后将错误返回给用户。
@csrf_exempt
def newsletters(request):
if request.method == 'POST':
sub = NewsletterUser(email=request.POST['email'], conf_num=random_digits())
try:
sub.save()
message = Mail(
from_email='pasiekaradosc@gmail.com',
to_emails=sub.email,
subject='Potwierdzenie Adresu Email w Newsletterze Pasieka Radość',
html_content= "<h1>Dziękujemy za zapisanie się do newslettera Pasieka Radość!<h1> \
<h2>Aby potwierdzić rejestrację newslettera</h2> \
<h2><a href='{}confirm/?email={}&conf_num={}'> Kliknij w poniższy link</a>.<h2>".format(request.build_absolute_uri(host),
sub.email,
sub.conf_num))
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
return render(request, 'newsletter/newsletter.html', {'email': sub.email, 'action': 'wysłano', 'form': NewsletterUserForm()})
except IntegrityError as ex:
# Return the error to your user
else:
return render(request, 'newsletter/newsletter.html', {'form': NewsletterUserForm()})
我已经在上面的代码中添加了 try/except
,当电子邮件不唯一时,您需要在 except
块中填写要执行的操作。