Question

I'm building a simple web app for New Year's Eve. I'm using Django and have an SQLite3 database set up.

The idea behind the web app is that someone submits a piece of text (finishing off a sentence on the homepage) using their Twitter username so that the sentence is then printed on the homepage with their associated @username and the user can also tweet it if they wish.

I need to understand how to...

  • Have a user log in via Twitter to authenticate their @username (probably using a wrapper such as python-twitter or one that you recommend)
  • Post the text and @username to database and save it with the date/time published
  • Get the @username and text from the database and print the sentence and @username (as the author) on the homepage in a feed of posts (Twitter feed style)

This is all new territory and I'm feeling the pressure as New Year's Eve is only days away. Any help would be much appreciated!

Was it helpful?

Solution

  1. To have users login / authenticate using twitter you'll need to play with the twitter API and Oauth. If you are looking for a ready-made solution i would recommend to you to look at Raymond Penners' django-allauth library which works out of the box. You can use this allauth tutorial to understand the basics of setting it up.

  2. Once you have the working allauth application with twitter, you'll need to create a model that will save the information such as The Message, Date & Time the Message was posted and the Owner of that message. A very simple model to achieve this could be something like the following.

    class Message(models.Model):

    """
    This is the message class that stores all the message
    objects.
    """
    
    message = models.TextField()
    time_posted = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User)
    

Once you have the model done, you'll need a form, where users will actually post the messages. The form could be a simple ModelForm that looks something like the following.

MessageForm(forms.ModelForm):
    """
    This is the base form for posting a message
    """

    class Meta:
        model = models.Message

        fields = ('message')

Now you'll need to pass the form to the view that renders the template. Maybe something like this. In the same view you can also take care of posting the message.

def post_message(request):
    form = forms.MessageForm()
    if request.method == 'POST':
        p_form = forms.MessageForm(request.POST)
        if p_form.is_valid():
            data = p_form.save(commit=False)
            data.owner = request.user
            data.save()
            return redirect('/success/')  # Redirect to a success page
        else:
            raise ValidationError('The Form did not validate')
    return render_to_response('post_message.html', {'form' : form}, context_instance=RequestContext(request))

Now you'll need to write a template named 'post_message' into your templates that will be called once the above view is requested. A very simple template will look like this.

{% if form.errors %}
    {{ form.errors %}
{% endif %}

<form action ="" method ="POST">
{% csrf_token %}
{{ form }}
<input type="submit" id="submit" value="Post Message" />
</form>

Once you have the template ready you can use a simple url (one as defined below) in your urls.py file to connect a requested url to your view. So say if you want the above view to be called when a user requests yourapp.com/post/ :

url(r'^post/$', 'yourapp.views.post_message', name="post"),

And that's it. I would also recommend you go through the Django's Basic Poll Application Tutorial to get more idea of how django entities work alltogether.

Hope the above example will help you in your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top