Django:使用用户的Twitter凭据填写表单数据

时间:2015-06-25 11:47:16

标签: python django python-2.7 twitter

我的应用程序有一个简单的表单,以及使用django-social-auth

实现的Twitter登录功能

forms.py

   from django import forms
from rango.models import Evangelized

class EvangelizedForm(forms.ModelForm):
    full_name = forms.CharField(help_text="Full Name")
    email = forms.CharField(help_text="Email ID")
    mobile_no = forms.CharField(help_text="Mobile number")
    twitter_url = forms.CharField(help_text="Twitter URL")
    gender = forms.CharField(widget=forms.RadioSelect(
                 choices=Evangelized.GENDER_CHOICES), help_text="Gender", max_length = 128)
    area_of_interest = forms.CharField(max_length = 1230, widget=forms.CheckboxSelectMultiple(
                                                    choices=Evangelized.AREA_CHOICES), help_text="Areas of interest(Upto 3)")
    """def clean_area_of_interest(self):
        if len(self.cleaned_data['area_of_interest']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['area_of_interest']"""
    other_area_of_interest = forms.Field(help_text="Other area of interest")
    city = forms.CharField(help_text="City")
    social_clout = forms.CharField(max_length = 1280, widget=forms.CheckboxSelectMultiple(choices=Evangelized.SOCIAL_CLOUT_CHOICES), help_text="I'd like to monetise my social clout because ")
    other_social_accounts = forms.CharField(widget=forms.RadioSelect(choices=Evangelized.BOOL_CHOICES), help_text="Do you have other social accounts?", max_length=128)
    questions = forms.CharField(help_text="Do you have any questions?", widget=forms.Textarea)
    about_yourself = forms.CharField(help_text="Tell us about yourself", widget=forms.Textarea)
    referrer = forms.CharField(help_text="I heard about this platform via:")

    class Meta:
        model = Evangelized
        fields = ('full_name', 'email', 'mobile_no', 'twitter_url', 'gender', 'area_of_interest', 'other_area_of_interest', 'city', 'social_clout', 'other_social_accounts',
                    'questions', 'about_yourself', 'referrer')

models.py

from django.db import models

class Evangelized(models.Model):
    full_name = models.CharField(max_length=128)
    email = models.EmailField()
    mobile_no = models.CharField(unique=True, max_length=128)
    twitter_url = models.CharField(unique=True, max_length=128)
    GENDER_CHOICES = (('M', 'Male'), ('F', 'Female'), ('U', 'Unisex/Parody'))
    gender = models.CharField(choices=GENDER_CHOICES, max_length = 128)
    AREA_CHOICES = (('Govt', 'Govt'), ('Entertainment', 'Entertainment'), ('Automobile', 'Automobile'),
                        ('Careers', 'Careers'), ('Books', 'Books'), ('Family', 'Family'), ('Food', 'Food'),
                            ('Gaming', 'Gaming'), ('Beauty', 'Beauty'), ('Sports', 'Sports'), ('Events', 'Events'),
                                ('Business', 'Business'), ('Travel', 'Travel'), ('Health', 'Health'), ('Technology','Technology'))
    area_of_interest = models.CharField(max_length = 1280)
    other_area_of_interest = models.CharField(blank=True, max_length=128)
    city = models.CharField(max_length=128)
    SOCIAL_CLOUT_CHOICES = (('one', 'I think I\'ve built it - earned it - deserve it'),
                                ('two', 'I\'d love to raise funds for my favorite charity.'),
                                    ('three', 'I\'ve already been doing it, more avenues won\'t hurt.'))
    social_clout = models.CharField(max_length = 1280)
    BOOL_CHOICES = (('Yes', 'Yes'), ('No', 'No'))
    other_social_accounts = models.CharField(choices=BOOL_CHOICES, max_length=128)
    questions = models.CharField(blank=True, max_length=1280)
    about_yourself = models.CharField(blank=False, max_length=1280)
    referrer = models.CharField(max_length=128)

views.py

from django.shortcuts import render
from django.http import HttpResponse
from rango.models import Evangelized
from rango.forms import EvangelizedForm
from django.http import HttpResponseRedirect, HttpResponse


def index(request):
    return HttpResponse("<a href = '/rango/fillform'>MicroCelebrity Form</a>")

def fillform(request):
    if request.method == 'POST':
        form = EvangelizedForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            form.errors
    else:
        form = EvangelizedForm()

    return render(request, 'rango/fillform.html', {'form': form})

fillform.html

<body>
        <h1>MicroCelebrity Form</h1><br>
        {% load twitter %}
        {% twitter_button %}    
        <br>
        <form id="evangelized_form" method="post" action="/rango/fillform/">

            {% csrf_token %}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {% for field in form.visible_fields %}
                {{ field.errors }}
                <b>{{ field.help_text }}</b><br>
                {{ field }}<br><br>
            {% endfor %}

            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

现在,我想从用户的Twitter凭据中获取电子邮件地址,姓名和Twitter帐户网址。所以基本上,在用户使用他的Twitter帐户详细信息登录后,我希望应用程序重定向回到表单,Full NameEmail IDTwitter URL的条目已经预先填充,以便用户不必再次输入详细信息,只需填写其他表单字段的详细信息。

我怎样才能做到这一点?任何线索将不胜感激。感谢。

0 个答案:

没有答案