Journal logo

Day 3 of posting about learning Python(Django) until I land a job.

Day 3 of posting

By Muhammad UsmanPublished 3 months ago 3 min read
1
Day3

Day 3: Building the Backstage - Users, Secrets, and the Symphony of Conversations

Greetings, fellow tech enthusiasts! Day three of my chatbot odyssey finds me venturing deep into the intricate world of the backend, crafting the essential elements that will power our conversational companion. Today's focus? Users, their secrets (well, not really, but secure logins!), and the all-important history of their interactions with the chatbot. Buckle up, as we delve into the fascinating realm of databases and user authentication!

The User Symphony: Composing the First Movement

Imagine a bustling concert hall, filled with eager audience members ready to engage with the music. In our chatbot's world, these audience members are the users. But before they can enjoy the symphony of conversation, we need a system to manage them effectively. This is where user management comes in, the maestro that ensures everyone has a seat and feels welcome.

To achieve this, I delved into the world of databases, opting for Django's built-in ORM (Object-Relational Mapper) as my trusty instrument. This powerful tool allows me to store user information like usernames, passwords (hashed securely, of course!), and any additional details we might need in the future, all in a structured and easily accessible manner.

Here's a glimpse into the code that orchestrates this user management system: This is just an example not the exact code. You can check for complete code on my GitHub.

#Python

from django.db import models

class User(models.Model):

username = models.CharField(max_length=255, unique=True)

password = models.CharField(max_length=255) # Hashed password stored securely

# Add other user details as needed

def __str__(self):

return self.username

This code snippet defines a basic user model, similar to a blueprint for each user we create. The username and password fields act as the essential instruments in this symphony, allowing users to identify themselves and securely access the chatbot. The password, however, is stored in a special encrypted format, ensuring it remains a secret, just like a hidden melody waiting to be revealed at the right moment.

Securing the Stage: The Gatekeeper of Conversations

Now, imagine a majestic concert hall with a strict security guard ensuring only authorized guests enter. In our chatbot's world, this guard is the login authentication system, responsible for verifying user identities. This vital component ensures that only registered users can access and interact with the chatbot, protecting its integrity and preventing unauthorized access.

To create this security system, I leveraged Django's built-in user authentication features. These features act as a sophisticated combination lock, requiring users to enter the correct username and password (the secret code) to gain access. Once their credentials are verified, the system unlocks the door, allowing them to enter the conversation hall and engage with the chatbot.

Preserving the Encore: The Symphony's Lasting Legacy

But the beauty of a concert lies not just in the performance itself, but also in the memories it creates. Similarly, the conversations between users and the chatbot hold valuable information and insights. To preserve these interactions, I implemented a chat history system.

This system utilizes another model, specifically designed to store chat messages:

#Python

from django.db import models

from .models import User

class Chat(models.Model):

user = models.ForeignKey(User, on_delete=models.CASCADE)

message = models.TextField()

timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):

return f"{self.user.username}: {self.message[:20]}" # Truncated preview

This model acts like a dedicated music score, recording each message exchanged between a user and the chatbot. The user field links each message to a specific user, while the message and timestamp fields capture the content and timing of each interaction. By storing these messages, we create a valuable historical record, allowing users to revisit past conversations and track the flow of information, similar to how concert attendees might cherish a recording of a performance.

Challenges and Triumphs: A Composer's Journey

As with any creative endeavor, composing this "backend symphony" wasn't without its challenges. I encountered moments of dissonance, grappling with complex database relationships and ensuring proper data validation. But with perseverance and the guidance of online resources, I was able to overcome these hurdles and successfully implement the core functionalities.

The Melody Continues: The Road Ahead

With the foundation laid for user management, authentication, and chat history, we've taken a significant step forward in building a robust and engaging chatbot. The next stage of our journey involves integrating these features with the chatbot itself, creating a seamless user experience where audiences can interact with the chatbot effortlessly.

Stay tuned for future blog posts as I share my progress in bringing this chatbot

Source Code for this project is on my GitHub.

industryVocalcareer
1

About the Creator

Muhammad Usman

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.