User authentication with either username or email.
Hi, I created a backends.py in my project folder. backends.py :: *from django.conf import settings* *from django.contrib.auth.models import User* * * *class EmailOrUsernameModelBackend(object):* *def authenticate(self, username=None, password=None):* *if '@' in username:* *kwargs = {'email': username}* *else:* *kwargs = {'username': username}* *try:* *user = User.objects.get(**kwargs)* *if user.check_password(password):* *return user* *except User.DoesNotExist:* *return None* * * *def get_user(self, user_id):* *try:* *return User.objects.get(pk=user_id)* *except User.DoesNotExist:* *return None* * * And in the views.py, *from django.contrib import auth* *from django.contrib.auth.models import User* *from tangle.backends import EmailOrUsernameModelBackend* * * *def authentication(request):* *username = request.POST.get('username', '')* *password = request.POST.get('password', '')* *user = authenticate(username=username, password=password)* *if user is not None:* *auth.login(request, user)* *return HttpResponseRedirect('/loggedin/')* *else:* *return HttpResponseRedirect('/invalid_login')* * * Settings.py *AUTHENTICATION_BACKENDS = (* *'backends.EmailOrUsernameModelBackend',* *'django.contrib.auth.backends.ModelBackend'* *)* * * Now when I login using either username or email it shows an error "global name 'authenticate' is not defined" So, I used *user = EmailOrUsernameModelBackend.authenticate(username=username, password=password)* * * It shows TypeError at /authentication/ unbound method authenticate() must be called with EmailOrUsernameModelBackend instance as first argument (got nothing instead) Can anyone help me out in solving the issue? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: User authentication with either username or email.
As you can see in the views.py I did import the class * EmailOrUsernameModelBackend.* * * Now when I used " *user = EmailOrUsernameModelBackend().authenticate(username=username, password=password) *" It gave an error saying: AttributeError at /tangle/auth/ 'User' object has no attribute 'backend' -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: User authentication with either username or email.
Sorry. The error was: AttributeError at /authentication/ 'User' object has no attribute 'backend' -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Importing the user.id in form.
Hi, I defined a model which has a 'ForeignKey' to the 'User' . models.py : *class Design(models.Model):* * title = models.CharField(max_length=100)* * designer=models.ForeignKey(User) * * description = models.TextField(null = True)* * def __unicode__(self):* * return self.title* * * forms.py : *class UploadForm(forms.ModelForm): * *class Meta:* *model = Design* *fields = ('title','description')* * * views.py : *def me(request):* *if request.POST:* *form = UploadForm(request.POST, request.FILES)* *if form.is_valid():* *form.save()* *return HttpResponseRedirect('/home/')* *else:* *return render_to_response('home/invalid.html')* *else:* *form = UploadForm()* *args = {}* *args.update(csrf(request))* *args['form'] = form* *return render_to_response('home/upload.html', args,context_instance=RequestContext(request))* * * Now when I save the save the form, the '*designer=models.ForeignKey(User)' *raises an error "Column 'designer_id' cannot be null" . A user after logging in can upload a new design. How to store the " designer_id " i.e, the user.id of the user who uploaded the design? Can someone help me!! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: User authentication with either username or email.
This helped alot and solved the issue. Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
Re: Importing the user.id in form.
That worked. Thanks alot. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.