Skip to main content

Important view.py file for using restful api django

from django.shortcuts import render
from django.conf import settings
import requests
from github import Github, GithubException
from .forms import DictionaryForm
def home(request):
is_cached = ('geodata' in request.session)
if not is_cached:
ip_address = request.META.get('HTTP_X_FORWARDED_FOR', '')
params = {'access_key': settings.IPSTACK_API_KEY}
response = requests.get('http://api.ipstack.com/%s' % ip_address, params=params)
request.session['geodata'] = response.json()
geodata = request.session['geodata']
return render(request, 'core/home.html', {
'ip': geodata.get('ip'),
'country': geodata.get('country_name', ''),
'latitude': geodata.get('latitude', ''),
'longitude': geodata.get('longitude', ''),
'api_key': settings.GOOGLE_MAPS_API_KEY,
'is_cached': is_cached
})
def github(request):
search_result = {}
if 'username' in request.GET:
username = request.GET['username']
url = 'https://api.github.com/users/%s' % username
response = requests.get(url)
search_was_successful = (response.status_code == 200) # 200 = SUCCESS
search_result = response.json()
search_result['success'] = search_was_successful
search_result['rate'] = {
'limit': response.headers['X-RateLimit-Limit'],
'remaining': response.headers['X-RateLimit-Remaining'],
}
return render(request, 'core/github.html', {'search_result': search_result})
def github_client(request):
search_result = {}
if 'username' in request.GET:
username = request.GET['username']
client = Github()
try:
user = client.get_user(username)
search_result['name'] = user.name
search_result['login'] = user.login
search_result['public_repos'] = user.public_repos
search_result['success'] = True
except GithubException as ge:
search_result['message'] = ge.data['message']
search_result['success'] = False
rate_limit = client.get_rate_limit()
search_result['rate'] = {
'limit': rate_limit.rate.limit,
'remaining': rate_limit.rate.remaining,
}
return render(request, 'core/github.html', {'search_result': search_result})
def oxford(request):
search_result = {}
if 'word' in request.GET:
form = DictionaryForm(request.GET)
if form.is_valid():
search_result = form.search()
else:
form = DictionaryForm()
return render(request, 'core/oxford.html', {'form': form, 'search_result': search_result}) Important Html
{% extends 'base.html' %}
{% block content %}
<h2>GitHub API</h2>
<form method="get">
<input type="text" name="username">
<button type="submit">search on github</button>
</form>
{% if search_result %}
{% if search_result.success %}
<p>
<strong>{{ search_result.name|default_if_none:search_result.login }}</strong> has
<strong>{{ search_result.public_repos }}</strong> public repositories.
</p>
{% else %}
<p><em>{{ search_result.message }}</em></p>
{% endif %}
<p>Rate limit: {{ search_result.rate.remaining }}/{{ search_result.rate.limit }}</p>
{% endif %}
{% endblock %}

Comments

Popular posts from this blog

Connect POSTgreSQL with Django

  First install all necessary step  $ sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib Second $ sudo su - postgres $ psql Create User name with password  $ CREATE USER user_name WITH PASSWORD ' password ';   Create New Database $ CREATE DATABASE database_name WITH OWNER user_name ; Give permission  $ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user_name ; Update settings.py  DATABASES = { ' default ' : { ' ENGINE ' : ' django.db.backends.postgresql_psycopg2 ' , ' NAME ' : 'database_name ' , ' USER ' : ' user_name ' , ' PASSWORD ' : ' password' , ' HOST ' : ' localhost ' , ' PORT ' : ' 5432 ' , } }  Then, python3 manage.py migrate and finally create super user python3 man age.py createsuperuser