Tutorial 1: Project Setup
Welcome to this tutorial on django-vectordb
! Our goal is to learn how to use vectordb
. To make it more relatable, we'll demonstrate the features of django-vectordb
by building a basic blogging site.
In this first lesson, we'll lay the groundwork for our blogging site by creating a model to store blog posts and developing basic views for creating, viewing, and examining the details of each post. This is the most work we have to do. Integrating django-vectordb
is very easy and straight forward.
As we delve deeper, we will explore the capabilities of django-vectordb
by integrating related blog suggestions on the details page, based on vector similarity searches. This will showcase the flexibility and easy of use and power of django-vectordb
.
In the later stages of our tutorial, we will add a search page to demonstrate the text search features. Finally, we'll configure a REST API endpoint that allows for efficient AJAX searching and remote hosting of vectordb.
Now, with the introduction out of the way, let's get coding started!
Project setup
Create a new Django project named tutorial
, then start a new app called blog
.
# Create a virtual environment to isolate our package dependencies locally
python3 -m venv env
source env/bin/activate # On Windows use `env\Scripts\activate`
# Install Django and VectorDB into the virtual environment
pip install django
pip install django-vectordb[standard] # include optional dependencies
# Set up a new project with a single application
django-admin startproject tutorial
cd tutorial
django-admin startapp blog
The project layout should look like:
tutorial
├── blog
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── tutorial
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
Now lets add blog to the INSTALLED_APPS
in tutorial/settings.py
# settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"blog", # <--
]
Next lets add a POST
model to blog app blog/models.py
for saving our posts
Our model has three fields, the title
, description
, created_at
, and a user
foreign key.
Now sync your database for the first time:
We'll also create an initial user named admin
with a password. We'll authenticate as that user later in our example.
Once you've set up a database and the initial user is created and ready to go.
Run the development server now
Adding Sample Blogs
Lets add the Post model to the admins panel. Edit the blog/admin.py
blog/admin.py | |
---|---|
Now we can visit http://127.0.0.1:8000/admin/blog/post/
and add a few Posts. Go ahead and add something
Forms
In the last section we used the admin panel to add some post. Now lets add a form for post to the blogs.
Create a new file in blog directory called forms.py
blog/forms.py | |
---|---|
Views
Lets add some views for viewing our blog posts. We will keep things simple and use function based views.
First create a directory called templates
withing the blog app. Inside the templates
directory create another folder called blog
.
Lets add a basic base.html
blog/templates/blog/base.html | |
---|---|
Now lets create the list, detail, create view.
And finally, lets add the templates for these views.
We will begin with a template that displays a list of blog posts with their titles as clickable links. The template extends the "blog/base.html" template. In case there are no posts, it displays the message "No posts available." At the end, we add a link to create a new blog post using the "post_create" URL pattern.
Next we add the template "post_detail.html" responsible for displaying the details of a single blog post. The template also includes a link to navigate back to the list of all blog posts.
blog/templates/blog/post_detail.html | |
---|---|
blog/templates/blog/post_create.html | |
---|---|
That covers the basic functionality we want to work with. However, there's still one more aspect to address. We need to ensure that users are associated with their respective blogs, which means providing a way for them to log in to the app for the post functionality to work correctly. As evident from the code, posting requires user authentication.
So lets create a quick login form in blog/templates/blog/login.html
blog/templates/blog/login.html | |
---|---|
URLs
Lets now add the urls for our views. Create a new file called urls.py
in the blog app.
This wraps up the boilerplate necessary for testing the Django vector database. Now when we run the server
And we can view the details of the post by clicking the title.
Summary
In this tutorial we create a basic project to work with Django Vector Database. We created all the basic views and forms we need to create and view posts. Next lets integrated django vector database