02.vedas and upanishads Record Search Project using Django Web Application – Part 02
Welcome to the second part of the 02.vedas and upanishads Record Search Project using Django Web Application series. In this article, we will continue building our Django web application to allow users to search for records related to Vedas and Upanishads.
Setting up the Django Models
In our previous article, we created the Django project and app. Now, let’s define the models that will represent the data related to Vedas and Upanishads. We will create models for Vedas, Upanishads, and Records.
from django.db import models
class Veda(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class Upanishad(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class Record(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
veda = models.ForeignKey('Veda', on_delete=models.CASCADE)
upanishad = models.ForeignKey('Upanishad', on_delete=models.CASCADE)
Creating Forms for User Input
Next, we will create forms that allow users to input data for Vedas, Upanishads, and Records. We will use Django’s built-in forms to create these input forms.
from django import forms
class VedaForm(forms.ModelForm):
class Meta:
model = Veda
fields = ['name', 'description']
class UpanishadForm(forms.ModelForm):
class Meta:
model = Upanishad
fields = ['name', 'description']
class RecordForm(forms.ModelForm):
class Meta:
model = Record
fields = ['title', 'description', 'veda', 'upanishad']
Creating Views for Searching Records
Finally, we will create views that will allow users to search for records related to Vedas and Upanishads. We will use Django’s QuerySet API to filter records based on user input.
from django.shortcuts import render
from .models import Record
def search_records(request):
if request.method == 'GET':
query = request.GET.get('query')
records = Record.objects.filter(title__icontains=query)
return render(request, 'search_results.html', {'records': records})
Conclusion
In this article, we continued building our Django web application for the 02.vedas and upanishads Record Search Project. We defined models for Vedas, Upanishads, and Records, created input forms, and implemented views for searching records. In the next part, we will add functionality to display search results and improve the user interface. Stay tuned!