Created
March 4, 2019 13:08
-
-
Save thejungwon/8e347ca155eb7183ae2e668e0dbe5ba6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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