在 Django 应用程序上连接 facebook 后选择用户名的功能
我正在研究 Django 应用程序与 facebook 的集成,我几乎做到了,但我想为用户提供一个工具,让他/她在我的 django 网站上成功登录 facebook 后选择他/她的用户名,这里是代码:
form for usename :
class RegisterUsernameForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'username'))
def clean_username(self):
"""
Validate that the username is alphanumeric and is not already
in use.
"""
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
也针对它制作了一个模板,
这是我用来使用
def register_username(request, form_class=RegisterUsernameForm, template_name='注册/register_username_form.html', extra_context=None):
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid():
username = form['username'].data
return HttpResponseRedirect(reverse('facebook_settings'))
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form },
context_instance=context)
其中我只是从...获取用户名...
我的 django 应用程序上有一个 fb_authenticate,其中我检查用户是否存在于我的数据库中,如果不存在,则在此时创建新的用户名我需要重定向上面提到的“用户名”表单。所以用户可以选择它,然后将他注册到 dgango 应用程序
类 FbBackend:
def fb_authenticate(self, request, facebook_id=None):
if not facebook_id: return None
try:
profile = UserProfile.objects.get(facebook_id=facebook_id)
#To Check whether the same user is loggin in to facebook if not then logout the perivous one and login new one.
if request.user != profile.user :
from django.contrib.auth import logout
logout(request)
request.user=profile.user
UpdateFbUserDetails(request, profile.user, facebook_id)
return profile.user
except UserProfile.DoesNotExist:
# No user. Create one.
pass
#Get user facebook account details like first name, last name and email address.
username = request.facebook.users.getInfo([request.facebook.uid], ['name'])[0]['name']
firstname = request.facebook.users.getInfo([request.facebook.uid], ['first_name'])[0]['first_name']
lastname = request.facebook.users.getInfo([request.facebook.uid], ['last_name'])[0]['last_name']
emailaddress = request.facebook.users.getInfo([request.facebook.uid], ['email'])[0]['email']
# Create the username for facebook logged in user
fbusername = (emailaddress.rsplit ('@')[0]).replace ('.','') + "_"+((emailaddress.rsplit ('@')[1])[0]).capitalize()
try:
user = User.objects.get(email=emailaddress)
# This shouldn't really happen. Log an error.
# logging.error('Strange: user %s already exists.' % username)
except User.DoesNotExist:
user = User.objects.create_user(fbusername,emailaddress,'xxxx')#('fb_%s' % facebook_id, '')
user.first_name = firstname
user.last_name = lastname
if not UpdateFbUserDetails(request, user, facebook_id):
return None
user.save()
profile, created = UserProfile.objects.get_or_create(user=user)
profile.facebook_id = facebook_id
new_basics_info = UserBasicInfo()
new_basics_info.full_name = username
new_basics_info.save()
profile.basics_info = new_basics_info
new_personal_info = UserPersonalInfo()
new_personal_info.save()
profile.personal_info = new_personal_info
profile.save()
return user
请帮助我如何实现它......
谢谢
Ansh J
I am working on integration of django application with facebook and i did almost but I want to give an facility to the user to select his/her username after he/she successfully logged in facebook on my django site here is the code :
form for usename :
class RegisterUsernameForm(forms.Form):
username = forms.RegexField(regex=r'^\w+
made an template against it as well
here is the view which i used to use the from
def register_username(request, form_class=RegisterUsernameForm,
template_name='registration/register_username_form.html',
extra_context=None):
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES)
if form.is_valid():
username = form['username'].data
return HttpResponseRedirect(reverse('facebook_settings'))
else:
form = form_class()
if extra_context is None:
extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{ 'form': form },
context_instance=context)
in which i am just getting the username from the from...
I have one fb_authenticate on my django application in which i am check whether user exist in my db or not if not then create new one at that point of time i need to redirect that form "username" which i have above mention . so user can select it and then will register him in dgango app
class FbBackend:
def fb_authenticate(self, request, facebook_id=None):
if not facebook_id: return None
try:
profile = UserProfile.objects.get(facebook_id=facebook_id)
#To Check whether the same user is loggin in to facebook if not then logout the perivous one and login new one.
if request.user != profile.user :
from django.contrib.auth import logout
logout(request)
request.user=profile.user
UpdateFbUserDetails(request, profile.user, facebook_id)
return profile.user
except UserProfile.DoesNotExist:
# No user. Create one.
pass
#Get user facebook account details like first name, last name and email address.
username = request.facebook.users.getInfo([request.facebook.uid], ['name'])[0]['name']
firstname = request.facebook.users.getInfo([request.facebook.uid], ['first_name'])[0]['first_name']
lastname = request.facebook.users.getInfo([request.facebook.uid], ['last_name'])[0]['last_name']
emailaddress = request.facebook.users.getInfo([request.facebook.uid], ['email'])[0]['email']
# Create the username for facebook logged in user
fbusername = (emailaddress.rsplit ('@')[0]).replace ('.','') + "_"+((emailaddress.rsplit ('@')[1])[0]).capitalize()
try:
user = User.objects.get(email=emailaddress)
# This shouldn't really happen. Log an error.
# logging.error('Strange: user %s already exists.' % username)
except User.DoesNotExist:
user = User.objects.create_user(fbusername,emailaddress,'xxxx')#('fb_%s' % facebook_id, '')
user.first_name = firstname
user.last_name = lastname
if not UpdateFbUserDetails(request, user, facebook_id):
return None
user.save()
profile, created = UserProfile.objects.get_or_create(user=user)
profile.facebook_id = facebook_id
new_basics_info = UserBasicInfo()
new_basics_info.full_name = username
new_basics_info.save()
profile.basics_info = new_basics_info
new_personal_info = UserPersonalInfo()
new_personal_info.save()
profile.personal_info = new_personal_info
profile.save()
return user
Please help me how i can achieve it......
Thanks
Ansh J
,
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'username'))
def clean_username(self):
"""
Validate that the username is alphanumeric and is not already
in use.
"""
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
made an template against it as well
here is the view which i used to use the from
def register_username(request, form_class=RegisterUsernameForm,
template_name='registration/register_username_form.html',
extra_context=None):
in which i am just getting the username from the from...
I have one fb_authenticate on my django application in which i am check whether user exist in my db or not if not then create new one at that point of time i need to redirect that form "username" which i have above mention . so user can select it and then will register him in dgango app
class FbBackend:
Please help me how i can achieve it......
Thanks
Ansh J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了......
我只是将所有保存代码放在视图文件中:
if form.is_valid(): 之后
当用户登录时,我将他导航到该页面,当他选择加入时,将在我的 django 应用程序中注册。
I have solve it ....
I just put all the saving code in view file after :
if form.is_valid():
and the when user is logged in I navigate him to that page and when he select join then will get registered in my django application .