have considered all your suggestions and have tried something like this. But it still fails. I have numerous errors, I guess. I have marked in yellow, potential issues.
Many Thanks if you can help me fix it Thanks for reading... Here is my forms.py ------- from django.newforms import * from django.contrib.auth.models import User class RegistrationForm(Form): username = RegexField(r'^[a-zA-Z0-9_]{3,30}$', max_length = 30) email= EmailField() password1 =CharField(min_length=6, max_length =20, widget= PasswordInput) password2 = CharField(min_length=6, max_length =20, widget= PasswordInput) def clean(self): if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: raise ValidationError(u'Please make sure your passwords match.') return self.clean_data def isValidUsername(self, field_data, all_data): try: User.objects.get (username=field_data) except User.DoesNotExist: return raise ValidationError('The username "%s" is already taken.' % field_data) def save(self, clean_data): u = User.objects.create_user(clean_data['username'], clean_data['email'], clean_data['password1']) u.is_active = False u.save() return u Here is my views.py -------- import datetime, random, sha from django.shortcuts import render_to_response, get_object_or_404 from django.core.mail import send_mail from django.newforms import * from django.contrib.auth.views import login as auth_login from quiz.quizapp.models import UserProfile from quiz.quizapp.forms import RegistrationForm def register(request): if request.user.is_authenticated(): # Do Not register users already there return render_to_response('quizapp/register.html', {'has_account': True}) form = RegistrationForm(request.POST) if request.POST: if form.is_valid(): # Save the user data = form.clean_data # Build the activation key for their account salt = sha.new(str(random.random())).hexdigest()[:5] activation_key = sha.new(salt+new_user.username).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) # Create and save their profile new_profile = UserProfile(user=new_user, activation_key=activation_key, key_expires=key_expires) new_profile.save() email_body = _('Hi, %s, thank you for registering for To confirm this registration click the link below within 48 hours \n\n \ http://quiz/userprofile/confirm/%s'<http://quiz/userprofile/confirm/%25s%27>) % ( new_user.username, new_profile.activation_key) # Send an email with the confirmation link email_subject = _('Confirmation of your new account) send_mail(email_subject, email_body, [new_user.email]) return render_to_response('quizapp/register.html', {'created': True}) form = form.clean_data # I am not able to get the Formwarapper return render_to_response('quizapp/register.html', {'form': form }) def confirm(request, activation_key): if request.user.is_authenticated(): return render_to_response('confirm.html', {'has_account': True}) user_profile = get_object_or_404(UserProfile, activation_key=activation_key) if user_profile.key_expires < datetime.datetime.today(): return render_to_response('confirm.html', {'expired': True}) user_account = user_profile.user user_account.is_active = True user_account.save() return render_to_response('confirm.html', {'success': True}) ---- Ramdas S On 1/5/07, Vadim Macagon <[EMAIL PROTECTED]> wrote:
Honza Kr l wrote: > Hi Ramdas, > the "Right Way (tm)" how to solve this using newforms is: > > 1) subclass Field to create a field representing (and validating) username: > > class UserField( forms.Field ): > def clean( self, value ): > do what you must to verify username, throw validation error if you > are not satisfied with the data I'd just like to point out that subclassing isn't the only way to add custom validation to a field, you can also implement the clean_FIELDNAME method on your form, or override the forms clean(), this is how I've done it: class RegistrationForm(forms.Form): username = forms.RegexField(r'^[a-zA-Z0-9_]{3,30}$', max_length = 30) <- rest of the fields here -> def clean_username(self): <- additional validation code here -> <- raise forms.ValidationError if data is invalid -> -+ enlight +- >
--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---