[PYTHON] Twitter posting application made with Django

Let's make an application that just posts to Twitter

It's a Django version of what I did with bottle. We will create a very rudimentary application that simply posts and returns the posted string. Since the script itself is burnt, it may be a mess with Python 2 series, but it works for the time being.

Past article http://qiita.com/Gen6/items/1848f8b4d938807d082e      http://qiita.com/Gen6/items/a5562c36fc5c67c89916      http://qiita.com/Gen6/items/e845787a6ad073a77310

Please do the preparation

(virtualenv)$ pip install requests requests_oauthlib

Install it in a virtual environment.

Application image

スクリーンショット 2016-10-24 12.39.17.png

I will make something like this. You can create a static folder for bootstrap and throw it in, or you can write the CSS yourself.

contents

myapp/views.py


from requests_oauthlib import OAuth1Session
import json
import re
import os
import requests
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

from django.http.response import HttpResponse
from django.shortcuts import render


def index(request):
    Message = {
        'words': request.GET.get('words'),
    }

    msg = request.GET.get('words')

    C_KEY = "***********************"
    C_SECRET = "***********************"
    A_KEY = "***********************"
    A_SECRET = "***********************"

    url = "https://api.twitter.com/1.1/statuses/update.json"
    params = {"status": msg,"lang": "ja"}
    tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
    req = tw.post(url, params = params)

    return render(request, 'index.html', Message)

myapp/urls.py


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^template/$', views.index, name='index'),
]

templates/index.html


{% extends "base.html" %}
{% block body %}
  <div class="container">
    <div class="row">
    <form action="" method="get" class="form-group">
      <label>Tweet<input type="text" size="20" name="words" class="form-control"></label>
      <input type="submit" class="btn btn-primary" value="Send">
    </form>
    {% if words %}
      <p>「{{ words }}I tweeted.</p>
    {% endif %}
    </div>
  </div>

{% endblock %}

Is it because of the feeling that it feels much easier than when it was a bottle? > Bottle version http://qiita.com/Gen6/items/ee33eb51fbeb969bb9db

Click here if you want to continue http://qiita.com/Gen6/items/11fa5265053da95fcf0b

Recommended Posts

Twitter posting application made with Django
Your own Twitter client made with Django
I made a WEB application with Django
Twitter OAuth with Django
Web application creation with Django
Web application made with Python3.4 + Django (Part.1 Environment construction)
Twitter posting client made with Flask with simple login function
Measure Django application coverage with Coverage.py
Deploy a Django application with Docker
Build a web application with Django
Twitter search client made with bottle
A simple RSS reader made with Django
Application development using SQLite with Django (PTVS)
I made CORS custom middleware with Django
Internationalization with django
CRUD with Django
Until you CI what you made with Django with Jenkins
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Create a one-file hello world application with django
Authenticate Google with Django
Upload files with Django
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Development digest with Django
I made a Twitter BOT with GAE (python) (with a reference)
Output PDF with Django
Markdown output with Django
Hit the Twitter API after Oauth authentication with Django
Posting tweets with python
Getting Started with Django 1
Container-like # 2 made with C
Send email with Django
File upload with django
Easy Twitter Posting Program
Use LESS with Django
Single sign-on to your Django application with AWS SSO
Pooling mechanize with Django
A memo about building a Django (Python) application with Docker
Deploy a Django app made with PTVS on Azure
Use MySQL with Django
Start today with Django
Getting Started with Django 2
I made a simple book application with python + Flask ~ Introduction ~
Star Wars release commemoration, Star Wars style TL application made with bottle
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Pomodoro timer made with Errbot
Minimal website environment with django
Create an API with Django
Twitter graphing memo with Python
Get Twitter timeline with python
Use Twitter API with Python
Extract Twitter data with CSV
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
SNS Flask (Ajax) made with Flask
Web application development with Flask
Create a homepage with django
WebSocket application with Flask-Socket IO