Home > Enterprise >  Django How to bring data into url from DB?
Django How to bring data into url from DB?

Time:09-21

Models.py

from django.db import models

# Create your models here.
class reviewData(models.Model):
    building_name = models.CharField(max_length=50)
    review_content = models.TextField()
    star_num = models.FloatField()
class buildingData(models.Model):
    building_name = models.CharField(max_length=50)
    building_loc = models.CharField(max_length=50)
    building_call = models.CharField(max_length=20)

views.py

# Create your views here.
from django.shortcuts import render
from rest_framework.response import Response
from .models import reviewData
from .models import buildingData
from rest_framework.views import APIView
from .serializers import ReviewSerializer

class BuildingInfoAPI(APIView):
    def get(request):
        queryset = buildingData.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)
class ReviewListAPI(APIView):
    def get(request):
        queryset = reviewData.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)

urls.py

from django.contrib import admin
from django.urls import path
from crawling_data.views import ReviewListAPI
from crawling_data.views import BuildingInfoAPI

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/buildingdata/', BuildingInfoAPI.as_view()),
    #path('api/buildingdata/(I want to put building name here)', ReviewListAPI.as_view())

]

I am making review api.

I want to use building name as url path to bring reviews for specific buildings

For example, there are a, b, c, d, e reviews a, b, c reviews are for aaabuilding d, e reviews are for aaabuilding

api/buildingdata/aaabuilding (only shows aaabuilding review) api/buildingdata/xxxbuilding (only shows xxxbuilding review)

I've searched some dynamic url posts, but they were not that i want.

Is there any way to bring building name into url from db?

CodePudding user response:

In urls.py

path('api/buildingdata/<str:building>/', BuildingInfoAPI.as_view()),

And in views.py

class BuildingInfoAPI(APIView):
    def get(request, building):
        queryset = buildingData.objects.filter(building_name__contains=building)
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)

Hope this will solve the issue

CodePudding user response:

I suggets you take a look at the django documentation, this is pretty well explained there, here is a useful link

In your urls.py, you should put:

from django.contrib import admin
from django.urls import path
from crawling_data.views import ReviewListAPI
from crawling_data.views import BuildingInfoAPI

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/buildingdata/', BuildingInfoAPI.as_view()),
    # added <str:building> to url
    path('api/buildingdata/<str:building>', ReviewListAPI.as_view())
]

And in your views.py:

# Create your views here.
from django.shortcuts import render
from rest_framework.response import Response
from .models import reviewData
from .models import buildingData
from rest_framework.views import APIView
from .serializers import ReviewSerializer

class BuildingInfoAPI(APIView):
    def get(request):
        queryset = buildingData.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)

class ReviewListAPI(APIView):
    # added another argument, retrieved from url
    def get(request, building):
        # changed .all() to .filter()
        queryset = reviewData.objects.filter(building_name = building)
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)
  • Related