-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linked the API endpoints from Django to the React Frontend
- Loading branch information
Showing
9 changed files
with
135 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
from django.db import models # Importing Django's models module to define database models | ||
from django.db import models | ||
|
||
# Category Model: Represents a category for Types | ||
class Category(models.Model): | ||
name = models.CharField(max_length=100) # 'name' field to store the category name with a max length of 100 characters | ||
name = models.CharField(max_length=100, unique=True) # Ensuring category names are unique to prevent duplicates | ||
|
||
# This method returns the name of the category when the model instance is printed | ||
def __str__(self): | ||
return self.name | ||
|
||
# Meta class is used to specify metadata about the model | ||
# Here, we specify the plural name of the model as 'categories' | ||
class Meta: | ||
verbose_name_plural = 'categories' | ||
|
||
# Type Model: Represents a specific type, associated with a category | ||
class Type(models.Model): | ||
image = models.ImageField(upload_to='uploads/types/') # 'image' field to store an image file, uploaded to the 'uploads/types/' directory | ||
category = models.ForeignKey(Category, on_delete=models.CASCADE, default=1) # ForeignKey field linking each Type to a Category; default is set to 1 | ||
name = models.CharField(max_length=200) # 'name' field to store the type name with a max length of 200 characters | ||
description = models.CharField(max_length=500, default='', blank=True, null=True) # Optional description field with max length of 500 characters | ||
image = models.ImageField(upload_to='uploads/types/') # Image field for storing uploaded images | ||
category = models.ForeignKey( | ||
Category, | ||
on_delete=models.CASCADE, | ||
related_name='types' # Optional: allows reverse access from Category to Type | ||
) | ||
name = models.CharField(max_length=200) | ||
description = models.CharField(max_length=500, default='', blank=True, null=True) | ||
|
||
# This method returns the name of the type when the model instance is printed | ||
def __str__(self): | ||
return self.name |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from rest_framework import serializers | ||
from .models import Type, Category | ||
|
||
class CategorySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Category | ||
fields = '__all__' | ||
|
||
class TypeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Type | ||
fields = '__all__' |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
from django.urls import path # Importing 'path' function for defining URL patterns | ||
from . import views # Importing the views module from the current directory | ||
"""artcategory URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/4.1/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.urls import path | ||
from .views import home # Replace 'home' with the actual view functions you have in views.py | ||
|
||
# Defining URL patterns for the app | ||
urlpatterns = [ | ||
# Mapping the root URL ('') to the 'home' view function | ||
# When a user visits the root of the site (e.g., '/'), the 'home' function will be executed | ||
path('', views.home, name='home'), | ||
path('', home, name='home'), # Ensure the view name matches what's in views.py | ||
] |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use client"; | ||
import { useState, useEffect } from 'react'; | ||
|
||
|
||
const MyPage = () => { | ||
const [message, setMessage] = useState(''); | ||
|
||
|
||
useEffect(() => { | ||
// Make API call to Django backend | ||
fetch('http://127.0.0.1:8000/') | ||
.then(response => response.json()) | ||
.then(data => setMessage(data.message)) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
}, []); | ||
|
||
|
||
return ( | ||
<div> | ||
<h1>{message}</h1> | ||
</div> | ||
); | ||
}; | ||
export default MyPage; |
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