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
(virtualenv)$ pip install requests requests_oauthlib
Install it in a virtual environment.
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.
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