Wie bei der Flasche denke ich, dass sich das Grundverständnis von Django vertiefen wird, wenn Sie mit der Erstellung eines Twitter-Clients beginnen. Übrigens werde ich etwas erstellen, mit dem Sie Beiträge, Antworten und Home-Timelines auf einem Bildschirm anzeigen können.
Das Ergebnis ist ein solches Bild.
Es wird eine Spezifikation wie sein.
Wenn Sie es unter ähnlichen Bedingungen ausprobieren möchten, lesen Sie bitte den vorherigen Artikel. http://qiita.com/Gen6/items/735245423b65698428be
.py
myapp/views.py
from requests_oauthlib import OAuth1Session
import time, calendar
import datetime
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):
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)
url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
params = {'count': 1}
req = tw.get(url, params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
limit = req.headers['x-rate-limit-remaining']
for tweet in timeline:
Text = (tweet['text'])
User = (tweet['user']['screen_name'])
Name = (tweet['user']['name'])
Img = (tweet['user']['profile_image_url'])
Created_at = YmdHMS(tweet['created_at'])
Message = {
'Words': msg,
'timeline': timeline,
'API_limit': limit,
'Text': Text,
'User': User,
'Name': Name,
'Img': Img,
'Created_at': Created_at,
}
return render(request, 'index.html', Message)
else:
Error = {
'Error_message': 'API eingeschränkt',
}
return render(request, 'index.html', Error)
def YmdHMS(created_at):
time_utc = time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
unix_time = calendar.timegm(time_utc)
time_local = time.localtime(unix_time)
return int(time.strftime('%Y%m%d%H%M%S', time_local))
YdmHMS.py
def YmdHMS(created_at):
time_utc = time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
unix_time = calendar.timegm(time_utc)
time_local = time.localtime(unix_time)
return str(time.strftime('%Y'+'Jahr'+'%m'+'Mond'+'%d'+'Tag'+'%H'+'Zeit'+'%M'+'Protokoll', time_local))
templates/base.html
<!DOCTYPE html>
<html lang="la">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../static/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/css/custom.css" rel="stylesheet">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
<title></title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
Es ist umständlich, eine solide URL zu schreiben.
templates/index.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-6">
<form action="" method="get" class="form-group">
<label><input type="text" size="60" name="words" class="form-control" placeholder="Tweet"></label>
<input type="submit" class="btn btn-primary" value="Senden">
<input type="button" class="btn btn-warning" value="Neu laden" onclick="location.reload();" />
</form>
{% if Words %}
<p>「{{ Words }}Ich habe getwittert.</p>
{% endif %}
<p class="error-message">{{ Error_message }}</p>
<p><span class="status">API-Limit:{{ API_limit }}</p>
</div>
<script type="text/javascript">
$(function() {
$(".reply").click(function() {
var newText = '@{{ User }} ';
$(':text[name="words"]').val(newText);
});
});
</script>
<div class="col-md-6">
{% if API_limit %}
<dl>
<dt><span class="reply"><img class="users-img" src="{{ Img }}">{{ User }}/{{ Name }}</span><span class="d-time">{{ Created_at }}</span></dt>
<dd>{{ Text }}</dd>
</dl>
<ul id="output"></ul>
{% endif %}
</div>
</div>
</div>
{% endblock %}
static/css/custom.css
.container {
width: 100%;
}
.row {
margin: 40px auto;
}
.status {
background: #E2264D;
border-radius: 4px;
color: #fff;
padding: 4px;
width: 100px;
text-align: center;
margin-right: 10px;
}
.reply {
cursor: pointer;
font-weight: bold;
}
.btn-primary {
margin-right: 2px;
}
.d-time {
margin-left: 10px;
border: 1px solid #d8d8d8;
border-radius: 4px;
padding: 4px;
font-weight: normal;
}
ul li {
list-style: none;
}
.users-img {
margin-right: 10px;
border-radius: 4px;
}
dt {
margin-bottom: 10px;
}
.error-message {
color: #ff3300;
}
Wenn Sie es in der Datenbank speichern möchten, lesen Sie bitte den folgenden Artikel. http://qiita.com/Gen6/items/907d869cdf1d588a4751
Erstellen Sie mit einem Bild wie oben.
Ich werde um Zeile 107 schreiben.
mysite/settings.py
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Es ist ein Muster, das das Posten, Erwähnen und Suchen implementiert. Da es in einer Ansichtsdatei geschrieben ist, ist es nicht einfach zu pflegen, aber beziehen Sie sich bitte darauf.
Sie können den SCHLÜSSEL nach draußen legen oder einfacher schreiben, aber er ist leicht zu verstehen. Probieren Sie verschiedene Dinge aus, um Ihren Code sauber zu halten.
Die Suchergebnisse sind ein in der Datenbank gespeichertes Muster.
myapp/views.py
from requests_oauthlib import OAuth1Session
import time, calendar
import datetime
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
from myapp.models import Supermodel
def index(request):
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)
url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
params = {'count': 1}
req = tw.get(url, params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
limit = req.headers['x-rate-limit-remaining']
for tweet in timeline:
Text = (tweet['text'])
User = (tweet['user']['screen_name'])
Name = (tweet['user']['name'])
Img = (tweet['user']['profile_image_url'])
Created_at = YmdHMS(tweet['created_at'])
Message = {
'Words': msg,
'timeline': timeline,
'API_limit': limit,
'Text': Text,
'User': User,
'Name': Name,
'Img': Img,
'Created_at': Created_at,
}
return render(request, 'index.html', Message)
else:
Error = {
'Error_message': 'API eingeschränkt',
}
return render(request, 'index.html', Error)
def search(request):
search_words = 'hogehoge'
search_words = request.GET.get('words')
C_KEY = '********************'
C_SECRET = '********************'
A_KEY = '********************'
A_SECRET = '********************'
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
url = 'https://api.twitter.com/1.1/search/tweets.json?'
params = {
'q': (search_words, 'utf-8'),
'lang': 'ja',
'count': '1'
}
req = tw.get(url, params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
limit = req.headers['x-rate-limit-remaining']
for tweet in timeline['statuses']:
Text = (tweet['text'])
User = (tweet['user']['screen_name'])
Name = (tweet['user']['name'])
Img = (tweet['user']['profile_image_url'])
Created_at = YmdHMS(tweet['created_at'])
data = Supermodel()
data.user_id = User
data.user_name = Name
data.user_img = Img
data.user_text = Text
data.user_created_at = Created_at
data.save()
Message = {
'Words': search_words,
'timeline': timeline,
'API_limit': limit,
'Text': Text,
'User': User,
'Name': Name,
'Img': Img,
'Created_at': Created_at,
}
return render(request, 'search.html', Message)
else:
Error = {
'Error_message': 'API eingeschränkt',
}
return render(request, 'search.html', Error)
def mentions(request):
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)
url = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json'
params = {'count': 1}
req = tw.get(url, params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
limit = req.headers['x-rate-limit-remaining']
for tweet in timeline:
Text = (tweet['text'])
User = (tweet['user']['screen_name'])
Name = (tweet['user']['name'])
Img = (tweet['user']['profile_image_url'])
Created_at = YmdHMS(tweet['created_at'])
Message = {
'Words': msg,
'timeline': timeline,
'API_limit': limit,
'Text': Text,
'User': User,
'Name': Name,
'Img': Img,
'Created_at': Created_at,
}
return render(request, 'mentions.html', Message)
else:
Error = {
'Error_message': 'API eingeschränkt',
}
return render(request, 'mentions.html', Error)
def YmdHMS(created_at):
time_utc = time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
unix_time = calendar.timegm(time_utc)
time_local = time.localtime(unix_time)
return int(time.strftime('%Y%m%d%H%M%S', time_local))
Der Vorgang zum Speichern in der Datenbank ist wie folgt. Ich denke, es ist einfach, leicht zu verstehen und leicht zu verstehen, was Sie tun.
data = Supermodel()
data.user_id = User
data.user_name = Name
data.user_img = Img
data.user_text = Text
data.user_created_at = Created_at
data.save()
Ich benutze Bootstrap.
templates/base.html
<!DOCTYPE html>
<html lang="la">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../static/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/css/custom.css" rel="stylesheet">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
<title></title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
templates/index.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<nav class="nav">
<ul>
<li><a href="{% url 'index' %}">POST</a></li>
<li><a href="{% url 'search' %}">SEARCH</a></li>
<li><a href="{% url 'mentions' %}">MENTIONS</a></li>
</ul>
</nav>
<div class="row">
<div class="col-md-6">
<form action="" method="get" class="form-group">
<label><input type="text" size="60" name="words" class="form-control" placeholder="Tweet"></label>
<input type="submit" class="btn btn-primary" value="Senden">
<input type="button" class="btn btn-warning" value="Neu laden" onclick="location.reload();" />
</form>
{% if Words %}
<p>「{{ Words }}Ich habe getwittert.</p>
{% endif %}
<p class="error-message">{{ Error_message }}</p>
<p><span class="status">API-Limit:{{ API_limit }}</p>
</div>
<script type="text/javascript">
$(function() {
$(".reply").click(function() {
var newText = '@{{ User }} ';
$(':text[name="words"]').val(newText);
});
});
</script>
<div class="col-md-6">
{% if API_limit %}
<dl>
<dt><span class="reply"><img class="users-img" src="{{ Img }}">{{ User }}/{{ Name }}</span><span class="d-time">{{ Created_at }}</span></dt>
<dd>{{ Text }}</dd>
</dl>
{% endif %}
</div>
</div>
</div>
{% endblock %}
templates/search.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<nav class="nav">
<ul>
<li><a href="{% url 'index' %}">POST</a></li>
<li><a href="{% url 'search' %}">SEARCH</a></li>
<li><a href="{% url 'mentions' %}">MENTIONS</a></li>
</ul>
</nav>
<div class="row">
<div class="col-md-6">
<form action="" method="get" class="form-group">
<label><input type="text" size="60" name="words" class="form-control" placeholder="Suche"></label>
<input type="submit" class="btn btn-primary" value="Senden">
<input type="button" class="btn btn-warning" value="Neu laden" onclick="location.reload();" />
</form>
{% if Words %}
<p>「{{ Words }}Wurde gesucht.</p>
{% endif %}
<p class="error-message">{{ Error_message }}</p>
<p><span class="status">API-Limit:{{ API_limit }}</p>
</div>
<script type="text/javascript">
$(function() {
$(".reply").click(function() {
var newText = '@{{ User }} ';
$(':text[name="words"]').val(newText);
});
});
</script>
<div class="col-md-6">
{% if API_limit %}
<dl>
<dt><span class="reply"><img class="users-img" src="{{ Img }}">{{ User }}/{{ Name }}</span><span class="d-time">{{ Created_at }}</span></dt>
<dd>{{ Text }}</dd>
</dl>
{% endif %}
</div>
</div>
</div>
{% endblock %}
templates/mentions.html
{% extends "base.html" %}
{% block body %}
<div class="container">
<nav class="nav">
<ul>
<li><a href="{% url 'index' %}">POST</a></li>
<li><a href="{% url 'search' %}">SEARCH</a></li>
<li><a href="{% url 'mentions' %}">MENTIONS</a></li>
</ul>
</nav>
<div class="row">
<div class="col-md-6">
<form action="" method="get" class="form-group">
<label><input type="text" size="60" name="words" class="form-control" placeholder="Tweet"></label>
<input type="submit" class="btn btn-primary" value="Senden">
<input type="button" class="btn btn-warning" value="Neu laden" onclick="location.reload();" />
</form>
{% if Words %}
<p>「{{ Words }}Ich habe getwittert.</p>
{% endif %}
<p class="error-message">{{ Error_message }}</p>
<p><span class="status">API-Limit:{{ API_limit }}</p>
</div>
<script type="text/javascript">
$(function() {
$(".reply").click(function() {
var newText = '@{{ User }} ';
$(':text[name="words"]').val(newText);
});
});
</script>
<div class="col-md-6">
{% if API_limit %}
<dl>
<dt><span class="reply"><img class="users-img" src="{{ Img }}">{{ User }}/{{ Name }}</span><span class="d-time">{{ Created_at }}</span></dt>
<dd>{{ Text }}</dd>
</dl>
{% endif %}
</div>
</div>
</div>
{% endblock %}
CSS
static/css/custom.css
.container {
width: 100%;
}
.nav {
background: rgba(0,0,0,0.7);
color: #fff;
width: 100%;
margin: 0 auto;
}
.nav ul li {
list-style: none;
margin: 10px auto;
}
.nav ul {
margin-left: -20px;
}
.nav li {
float: left;
margin-right: 10px!important;
}
.nav li a:link, .nav li a:visited {
color: #fff!important;
}
.row {
margin: 40px auto;
}
.status {
background: #E2264D;
border-radius: 4px;
color: #fff;
padding: 4px;
width: 100px;
text-align: center;
margin-right: 10px;
}
.reply {
cursor: pointer;
font-weight: bold;
}
.btn-primary {
margin-right: 2px;
}
.d-time {
margin-left: 10px;
border: 1px solid #d8d8d8;
border-radius: 4px;
padding: 4px;
font-weight: normal;
}
ul li {
list-style: none;
}
.users-img {
margin-right: 10px;
border-radius: 4px;
}
dt {
margin-bottom: 10px;
}
.error-message {
color: #ff3300;
}
myapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^search/', views.search, name='search'),
url(r'^mentions/', views.mentions, name='mentions'),
]
Wenn Sie die Tweet-Suchergebnisse nicht in der Datenbank speichern, benötigen Sie Folgendes nicht, sondern nur für den Fall, dass es in views.py beschrieben wird. Da SQLITE verwendet wird, belassen Sie die Grundeinstellungen.py als Standard.
myapp/models.py
from django.db import models
class Supermodel(models.Model):
user_name = models.CharField(max_length=140)
user_id = models.CharField(max_length=140)
user_img = models.CharField(max_length=140)
user_text = models.TextField(null=True)
user_created_at = models.CharField(max_length=140)
def __str__(self):
return self.user_name
myapp/admin.py
from django.contrib import admin
from myapp.models import Supermodel
class SupermodelAdmin(admin.ModelAdmin):
list_display = ('id','user_id','user_name','user_img','user_text','user_created_at')
admin.site.register(Supermodel,SupermodelAdmin)
Es sieht aus wie das.
Danach migrieren Sie bitte den Server und führen ihn aus.
Recommended Posts