From 0471560acc9334da52f88c49ff17963027f08227 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Fri, 26 Apr 2024 13:12:57 +0300 Subject: [PATCH 1/9] Added a method to get random dog facts using API --- django_project/views.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/django_project/views.py b/django_project/views.py index f9042b4c..6fe3fe43 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -2,16 +2,16 @@ from django.shortcuts import render def home(request): - # USING APIS => Example 1 - response = requests.get('https://api.github.com/events') - data = response.json() - result = data[0]["repo"] + # generating random dog images + dog_image_response = requests.get('https://dog.ceo/api/breeds/image/random') + dog_image = dog_image_response.json() + random_dog_image = dog_image['message'] + + return render(request, 'index.html', {'random_dog_image': random_dog_image}) - # Example 2 - reponse2 = requests.get('https://dog.ceo/api/breeds/image/random') - data2 = reponse2.json() - result2 = data2["message"] - - - - return render(request, 'templates/index.html', {'result': result, 'result2': result2}) \ No newline at end of file +def get_random_dog_fact(request): + response = requests.get('https://api.dog-facts-api.dog/facts/random') + data = response.json() + random_dog_fact = data['fact'] + + return render(request, 'index.html', {'random_dog_fact': random_dog_fact}) From 5fc92c21c2c0740d92cb86cc5036140d5aba0e4a Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Fri, 26 Apr 2024 13:26:42 +0300 Subject: [PATCH 2/9] Adjusted the image width --- django_project/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_project/views.py b/django_project/views.py index 6fe3fe43..e519fdd5 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -9,6 +9,8 @@ def home(request): return render(request, 'index.html', {'random_dog_image': random_dog_image}) + # generating facts + def get_random_dog_fact(request): response = requests.get('https://api.dog-facts-api.dog/facts/random') data = response.json() From 1aa0a00b6d837c3e755deaca87e54a771c22f766 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 02:56:20 +0300 Subject: [PATCH 3/9] made changes by introducing a feature to allow user guess the dog breed. Removed the random fact API due not functioning --- django_project/views.py | 45 ++++++++++++++++++++++++++++------------- static/styles.css | 12 +++++++---- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/django_project/views.py b/django_project/views.py index e519fdd5..7e4d3f38 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -1,19 +1,36 @@ import requests from django.shortcuts import render +from django.http import HttpResponse -def home(request): - # generating random dog images - dog_image_response = requests.get('https://dog.ceo/api/breeds/image/random') - dog_image = dog_image_response.json() - random_dog_image = dog_image['message'] - - return render(request, 'index.html', {'random_dog_image': random_dog_image}) +def index(request): + # Generating random dog breed images + response = requests.get('https://dog.ceo/api/breeds/image/random') + data = response.json() + random_dog_image = data['message'] - # generating facts + feedback = None # Initializing feedback variable -def get_random_dog_fact(request): - response = requests.get('https://api.dog-facts-api.dog/facts/random') - data = response.json() - random_dog_fact = data['fact'] - - return render(request, 'index.html', {'random_dog_fact': random_dog_fact}) + breed_name = request.GET.get('breed_name') + + if breed_name: + # Make API request to fetch breed information + url = f'https://api.thedogapi.com/v1/breeds/search?q={breed_name}' + breed_response = requests.get(url) + breed_data = breed_response.json() + breed_info = breed_data[0] if breed_data else None + + if breed_info: + return render(request, 'templates/index.html', {'random_dog_image': random_dog_image, 'breed_info': breed_info}) + else: + error_message = 'Breed information not found.' + return render(request, 'templates/index.html', {'random_dog_image': random_dog_image, 'error_message': error_message}) + + if request.method == 'POST': + user_guess = request.POST.get('user_guess').lower() + actual_breed = data['message'].split('/')[-2].replace('-', ' ') + if user_guess == actual_breed.lower(): + feedback = 'Correct guess!' + else: + feedback = f'Incorrect guess! The actual breed is {actual_breed}.' + + return render(request, 'templates/index.html', {'random_dog_image': random_dog_image, 'feedback': feedback}) diff --git a/static/styles.css b/static/styles.css index 530d510b..8364aefc 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,9 +1,13 @@ -body{ - background: hotpink +.container{ + max-width: 800px; + margin: 0 auto; + padding: 20px; + background-color: darkcyan; + border-radius: 5px; } img{ - width: 100px; + width: 120px; height: 100px; border-radius: 9999; -} +} \ No newline at end of file From fb18268a2ad331852a6e869b421d2e82c9da5071 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 03:06:42 +0300 Subject: [PATCH 4/9] Updated the index.html file to display dog info --- django_project/views.py | 6 +++--- templates/index.html | 48 +++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/django_project/views.py b/django_project/views.py index 7e4d3f38..2b8551c3 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -2,8 +2,8 @@ from django.shortcuts import render from django.http import HttpResponse -def index(request): - # Generating random dog breed images +def home (request): + response = requests.get('https://dog.ceo/api/breeds/image/random') data = response.json() random_dog_image = data['message'] @@ -13,7 +13,7 @@ def index(request): breed_name = request.GET.get('breed_name') if breed_name: - # Make API request to fetch breed information + url = f'https://api.thedogapi.com/v1/breeds/search?q={breed_name}' breed_response = requests.get(url) breed_data = breed_response.json() diff --git a/templates/index.html b/templates/index.html index b0f60817..5d6c3223 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,22 +1,42 @@ -{% load static%} +{% load static %} - - - - - - Document + + + + Random Dog Image, Guess the Breed, and Breed Information -

Hello, My name is Evans

+
+

Random Dog Image

+ Random Dog - {{result}} +

Guess the Breed

+
+ {% csrf_token %} + + + +
-

Random Pet

-{{result2}} - random pet - + {% if feedback %} +

{{ feedback }}

+ {% endif %} +
+ + {% if breed_info %} +
+

{{ breed_info.name }}

+

Temperament: {{ breed_info.temperament }}

+

Size: {{ breed_info.size }}

+

Lifespan: {{ breed_info.life_span }}

+ +
+ {% else %} +
+

{{ error_message }}

+
+ {% endif %} - \ No newline at end of file + From a854439f529a6b261e62bb7f279a31ff84ddbf42 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 03:17:26 +0300 Subject: [PATCH 5/9] Udjusted the image-width and background-color in styles.css --- static/styles.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/styles.css b/static/styles.css index 8364aefc..a3faecd3 100644 --- a/static/styles.css +++ b/static/styles.css @@ -2,12 +2,12 @@ max-width: 800px; margin: 0 auto; padding: 20px; - background-color: darkcyan; + background-color: yellowgreen; border-radius: 5px; } img{ - width: 120px; + width: 150px; height: 100px; border-radius: 9999; } \ No newline at end of file From af869453bf6e0d4831519eb65f2d27d573a43d63 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 08:19:29 +0300 Subject: [PATCH 6/9] Implemented timeout request handling in my Api requests to avoid long waiting times --- django_project/settings.py | 3 ++- django_project/views.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/django_project/settings.py b/django_project/settings.py index 85d47deb..39c27721 100644 --- a/django_project/settings.py +++ b/django_project/settings.py @@ -25,7 +25,8 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [".replit.dev", ".replit.app"] +ALLOWED_HOSTS = [".replit.dev", ".replit.app", + "127.0.0.1"] CSRF_TRUSTED_ORIGINS = ["https://*.replit.dev", "https://*.replit.app"] # Application definition diff --git a/django_project/views.py b/django_project/views.py index 2b8551c3..ff30b3cb 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -4,7 +4,7 @@ def home (request): - response = requests.get('https://dog.ceo/api/breeds/image/random') + response = requests.get('https://dog.ceo/api/breeds/image/random', timeout=5) data = response.json() random_dog_image = data['message'] From 259306ac9d31907d26c1411ff905c31bcb136938 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 08:50:46 +0300 Subject: [PATCH 7/9] Added a timeout to the dog breed search api to avoid long waitin --- django_project/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_project/views.py b/django_project/views.py index ff30b3cb..ab63a7e3 100644 --- a/django_project/views.py +++ b/django_project/views.py @@ -15,7 +15,7 @@ def home (request): if breed_name: url = f'https://api.thedogapi.com/v1/breeds/search?q={breed_name}' - breed_response = requests.get(url) + breed_response = requests.get(url, timeout=5) breed_data = breed_response.json() breed_info = breed_data[0] if breed_data else None From 4b3a64f35e5b5e52e709442e0b76596b16f3e965 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 09:05:05 +0300 Subject: [PATCH 8/9] Readjusted background color and image height add width in styles.css --- static/styles.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/static/styles.css b/static/styles.css index a3faecd3..d841a0c3 100644 --- a/static/styles.css +++ b/static/styles.css @@ -2,12 +2,12 @@ max-width: 800px; margin: 0 auto; padding: 20px; - background-color: yellowgreen; + background-color: burlywood; border-radius: 5px; } img{ - width: 150px; - height: 100px; + width: 130px; + height: 130px; border-radius: 9999; } \ No newline at end of file From 009baafbc355d750e94dc99b7d1f221878fa9079 Mon Sep 17 00:00:00 2001 From: CappyGerald Date: Sat, 27 Apr 2024 09:12:47 +0300 Subject: [PATCH 9/9] registred 127.0.0.1 in the settings.py --- static/styles.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/styles.css b/static/styles.css index d841a0c3..84428255 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,5 +1,5 @@ .container{ - max-width: 800px; + max-width: 700px; margin: 0 auto; padding: 20px; background-color: burlywood;