2016-05-18 7 views
5

के साथ ईमेल मैं अद्वितीय ईमेल पते के साथ django-registration का उपयोग कर रहा हूं और यदि उपयोगकर्ता द्वारा अनुरोध किया गया है और खाता पहले से सक्रिय नहीं है, तो मैं एक ईमेल में दूसरा सक्रियण फ़ॉर्म भेजने में सक्षम होना चाहता हूं। मुझे यह link स्टैक ओवरफ्लो पर मिला है लेकिन मुझे समझ में नहीं आता कि मुझे दिनचर्या कहां रखना चाहिए। मैं इसे अपने एप्लिकेशन की views.py में डाल दिया है और मैं त्रुटिDjango- पंजीकरण सक्रियण दोबारा नया कोड

global name 'RegistrationProfile' is not defined 

है जो हो रही है क्योंकि RegistrationProfile पंजीकरण में एक मॉडल है लेकिन यह मेरी डेटाबेस में एक मेज/रिकॉर्ड बनाने नहीं जब एक सक्रियण करता है फॉर्म भेजा जाता है इसलिए मुझे समझ में नहीं आता कि मैं इसे कैसे एक्सेस कर सकता हूं।

किसी भी सलाह का स्वागत किया जाएगा।

+0

क्या आपने पंजीकरण से पंजीकरणप्रोफाइल आयात करने का प्रयास किया – marcusshep

+0

आपका क्या मतलब है सक्रियण फ़ॉर्म पुनः भेजें? यह कोई समझ नहीं आता है। क्या आपका मतलब सक्रियण ईमेल भेजना था? शायद यह है कि आपके प्रश्न को – e4c5

+0

जारी करने से पहले आपके प्रश्न का उत्तर नहीं मिला है, मैंने पंजीकरणप्रोफाइल आयात करने का प्रयास किया है और हां, मैं पंजीकरण (मॉडल) से पहले शामिल हूं (या नया) सक्रियण ईमेल – HenryM

उत्तर

2

ठीक है, तो यह वह समाधान है जिसके साथ मैं आया हूं। यह सबसे सुंदर नहीं हो सकता है, लेकिन यह मेरे लिए काम कर रहा है।

यह views.py को

from .forms import ResendActivationEmailForm 
from django.core import signing 
from django.contrib.sites.shortcuts import get_current_site 
from django.template.loader import render_to_string 


def resend_activation_email(request): 

    email_body_template = 'registration/activation_email.txt' 
    email_subject_template = 'registration/activation_email_subject.txt' 

    if not request.user.is_anonymous(): 
     return HttpResponseRedirect('/') 

    context = Context() 

    form = None 
    if request.method == 'POST': 
     form = ResendActivationEmailForm(request.POST) 
     if form.is_valid(): 
      email = form.cleaned_data["email"] 
      users = User.objects.filter(email=email, is_active=0) 

      if not users.count(): 
       form._errors["email"] = ["Account for email address is not registered or already activated."] 

      REGISTRATION_SALT = getattr(settings, 'REGISTRATION_SALT', 'registration') 
      for user in users: 
       activation_key = signing.dumps(
        obj=getattr(user, user.USERNAME_FIELD), 
        salt=REGISTRATION_SALT, 
        ) 
       context = {} 
       context['activation_key'] = activation_key 
       context['expiration_days'] = settings.ACCOUNT_ACTIVATION_DAYS 
       context['site'] = get_current_site(request) 

       subject = render_to_string(email_subject_template, 
            context) 
       # Force subject to a single line to avoid header-injection 
       # issues. 
       subject = ''.join(subject.splitlines()) 
       message = render_to_string(email_body_template, 
              context) 
       user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) 
       return render(request, 'registration/resend_activation_email_done.html') 

    if not form: 
     form = ResendActivationEmailForm() 

    context.update({"form" : form}) 
    return render(request, 'registration/resend_activation_email_form.html', context) 

यह

class ResendActivationEmailForm(forms.Form): 
    email = forms.EmailField(required=True) 

forms.py लिए मैं गया है resend_activation_email_form.html बुलाया पंजीकरण में एक नया टेम्पलेट जो प्रयोग किया जाता है जोड़ा जाता है जोड़ा जाता है जब ईमेल परेशान हो गया है लेकिन मैंने ईमेल भेजने के लिए बिल्कुल वही टेम्पलेट का उपयोग किया है।

मैंने इसे ईमेल पर आधारित किया है क्योंकि मैंने अद्वितीय ईमेल का उपयोग किया है, लेकिन यह संभवतः उपयोगकर्ता नाम पर आधार के लिए अधिक समझदार है क्योंकि इसे डीजेंगो-पंजीकरण द्वारा अद्वितीय के रूप में परिभाषित किया गया है।

संबंधित मुद्दे