The backend for the Fruit Store website is designed to manage server-side operations for a seamless online shopping experience for fresh fruits and vegetables. Built with Django, it handles user authentication, product management, and more, making the application a fullstack solution.
The backend uses several technologies and libraries:
settings.py
)Features Implemented:
Future Work:
To send emails using Django, you'll need to configure its email backend. There are various email service providers, such as Mailgun and SendGrid which offer solutions for sending emails. However, on free plans, these services often place you in a sandbox environment with restrictions, such as limited sending capabilities or the need to verify recipient addresses.
A popular alternative is to use Gmail, which is free and allows you to send emails without these sandbox limitations. However, Gmail does have sending limits—typically 500 emails per day for regular accounts and 2,000 emails per day for Google Workspace accounts. To use Gmail with Django we first need to adjust our Gmail account settings to allow Django to send emails on its behalf. This includes enabling access for an App Password
if you have two-factor authentication enabled.
To begin you need to generate an app password for Gmail, this is necessary when setting up your Google account on a device or in an application that doesn't support two-step verification. Without enabling 2-factor authentication App passwords
will not be available.
authentication code
for and hit create
yele liof uwkg qdvc
app password
and any apps using it will have its access revoked. You can do this at any time in your Google Account under App passwords.Open up the .env.example
and copy the following block into the .env
file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@gmail.com' # your gmail account
EMAIL_HOST_PASSWORD = 'your-app-password' # Use the generated App Password here
Replace the EMAIL_HOST_USER
and EMAIL_HOST_PASSWORD
with your email and 16 digit app. Note, you only need to change
the last two if you are using gmail, however if you are using a different email provider you need to the entire block with their email services providers
To ensure the security of your Django project, you must set a SECRET_KEY
in your environment variables. This key is critical for various cryptographic operations in Django, including session management, password hashing, and more.
.env
FileCreate a .env
File:
.env
file in the root of your project directory. The .env
file should reside outside the 'src' directory where requirements.txt
and .env.example
are located..env.example
file as a guide. Copy its contents into your new .env
file and replace any placeholder values as needed.How to Set the Secret Key:
generateSessionKey()
function found in the utils
module of this project to generate a secure key..env
Once you have your secret key, add it to the .env
file as follows:
SECRET_KEY='your_generated_secret_key_here'
Download and Install PostgreSQL:
Create a Database:
psql
command-line tool to create a new database for your application.Configure Your Application:
Copy the following .env.example
file to the .env
and replace it with your postgresl setup:
Update the .env
file with your database configuration:
DB_NAME=your_database_name
DB_USER=your_database_user
DB_PASSWORD=your_database_password
DB_HOST=localhost
DB_PORT=5432
Run python manage.py migrate:
Your application should now be set up with PostgreSQL.
USE_LOCAL_DB
in settings.py
settings.py
file, locate the USE_LOCAL_DB
variable and manually set its value to True
.
This will configure the application to use the local database instead of the external one..env
file is not processing the value of the USE_LOCAL_DB
variable correctly.
It appears as though the variable doesn't exist, causing issues when trying to load the setting from the environment, so to rectify
this manually set it to True
for local and False
for external.Enhance your Django development experience by enabling Django-HTML syntax highlighting in Visual Studio Code (VS Code). This guide will walk you through the installation process to get the best out of your Django templates with proper syntax highlighting, auto-completion, and error checking.
When working on Django projects, your HTML templates often contain Django-specific syntax, such as {% %}
for template tags and {{ }}
for expressions. By enabling Django-HTML syntax highlighting in VS Code, you get:
Follow these steps to set up Django-HTML syntax highlighting in VS Code:
If you haven't already installed VS Code, download it from the official website and follow the installation instructions for your operating system.
To enable Django-HTML syntax highlighting, you'll need to install a VS Code extension. Here's how:
Ctrl+Shift+X
(Windows/Linux) or Cmd+Shift+X
(Mac)..html
file in your Django project.HTML
).To further enhance your Django development experience, consider installing these additional extensions:
If you face any issues with syntax highlighting:
.html
extension.HTML
a dropdown will now appear with auto-detect
on top, now in the search bar enter Django-HTML
and hit
Enter
now any Django snippets will be highlighted with the proper colour and syntaxTo manage your Django application through the admin interface, you need to create a superuser. Follow these steps:
Activate your virtual environment (if not already activated):
source venv/bin/activate
venv\Scripts\activate
Navigate to your project directory (where manage.py
is located):
cd src
Run the createsuperuser
command:
python manage.py createsuperuser
Enter the required details:
After completing these prompts, Django will create your superuser account.
Run the development server (if it's not already running):
python manage.py runserver
Open the Django admin interface in your web browser:
http://127.0.0.1:8000/admin/
Log in with the superuser credentials you created.
Once logged in, you'll have full access to manage your Django project's data and settings through the admin interface.
## Overview
To set up the backend environment, follow these instructions:
```bash
# Clone the repository
git clone https://github.com/EgbieAndersonUku1/Fullstack-fruit-and-veg.git .
# The period at the end specifies that the repository should be cloned into the current directory.
# Set up a virtual environment
python3 -m venv venv
# Activate the virtual environment you should see (venv) in front of the path
# For Unix or MacOS
source venv/bin/activate
# For Windows
venv\Scripts\activate
# Navigate to the src folder (make sure you are in the same directory as requirements.txt)
cd src
# Install dependencies
pip install -r requirements.txt
# Run migrations
python manage.py migrate
# Run the development server
python manage.py runserver
# Navigate to the application
http://127.0.0.1:8000/