Skip to content

Instantly share code, notes, and snippets.

@thejungwon
Created March 4, 2019 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejungwon/8e347ca155eb7183ae2e668e0dbe5ba6 to your computer and use it in GitHub Desktop.
Save thejungwon/8e347ca155eb7183ae2e668e0dbe5ba6 to your computer and use it in GitHub Desktop.
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from .models import *
import datetime
import base64
import requests
def index(request):
p = Post(date=datetime.datetime.now(), photo=random_picture())
p.save(force_insert=True)
posts = Post.objects.filter().order_by('-date')
return render(request, 'index.html', {'posts':posts})
def random_picture():
url = "https://source.unsplash.com/random/400x400"
response = requests.get(url, stream=True)
encoded_string = base64.b64encode(response.content)
del response
return encoded_string.decode('ascii')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment