मेरे पास वर्तमान में ऐसा मॉडल है
class Newsletter(models.Model):
email = models.EmailField(null=False, blank=True, max_length=200, unique=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)"
और मेरे पास कक्षा आधारित दृश्य है
class NewsletterView(SuccessMessageMixin, CreateView):
template_name = 'newsletter.html'
success_url = reverse_lazy('newsletter')
form_class = NewsletterRegisterForm
success_message = "Check your inbox for the verification email"
def form_valid(self, form):
self.conf_num = random_digits()
subject = 'Newsletter Confirmation',
html_content = 'Thank you for signing up for my email newsletter! \
Please complete the process by \
<a href="{}/confirm/?email={}&conf_num={}"> clicking here to \
confirm your registration</a>.'.format(self.request.build_absolute_uri('/confirm/'),
self.email,
self.conf_num)
sender = "noreply@example.com"
recipient = form.cleaned_data['email']
msg = EmailMultiAlternatives(subject, html_content, sender, [recipient])
msg.send()
return super().form_valid(form)
मैं थोड़ा उलझन में हूं कि मैं कक्षा आधारित दृश्य, conf_num
के माध्यम से कैसे सेट कर पाऊंगा? क्या मुझे अपने form_valid
फ़ंक्शन में self.conf_num = number
को सही ढंग से कॉल करना होगा?
जब मैं इन विधियों में से किसी एक को आजमाता हूं तो मुझे लगता है कि ईमेल अद्वितीय नहीं है या न्यूजलेटर ऑब्जेक्ट में कोई ईमेल नहीं है। किसी भी मदद की सराहना की जाएगी।
2 जवाब
इस मामले में, प्रपत्र वह वस्तु है जिसमें न्यूज़लेटर इंस्टेंस होता है।
def form_valid(self, form):
form.conf_num = random_digits()
newsletter = form.save()
मैं इस विधि को चुनूंगा,
class NewsletterView(SuccessMessageMixin, CreateView):
template_name = 'newsletter.html'
success_url = reverse_lazy('newsletter')
form_class = NewsletterRegisterForm
success_message = "Check your inbox for the verification email"
def send_email(self, conf_num):
# gather relevant data for email compose
# you can use function args or instance attributes
# and then, send mail from here
email.send()
def form_valid(self, form):
response = super().form_valid(form) # calling the `super()` method on the top will be the best, in this case
conf_num = random_digits()
self.send_email(conf_num)
# after sending the mail, access the `self.object` attribute
# which hold the instance which just created
self.object.conf_num = conf_num # assign the value
self.object.save() # call the save() method to save the value into the database
return response
मुझे आशा है कि टिप्पणियाँ यहाँ स्व-व्याख्यात्मक हैं :)
संबंधित सवाल
नए सवाल
django
Django एक ओपन-सोर्स सर्वर-साइड वेब एप्लीकेशन फ्रेमवर्क है जिसे पायथन में लिखा गया है। यह कम कोड, विशेष-अतिरेक पर विशेष ध्यान देने और निहित से अधिक स्पष्ट होने के साथ जटिल डेटा-संचालित वेबसाइटों और वेब एप्लिकेशन बनाने के लिए आवश्यक प्रयास को कम करने के लिए डिज़ाइन किया गया है।