This project uses:
- Django python web framework
- MySQL for Database
- Creating a virtual environment:
To create a Virtual Envionment
# install virtualenv
pip install virtualenv
# create a virtual env. with any name
virtualenv environment_name
# activate virtual env
# for git bash/cmd
source environment_name/bin/activate
# deactivate
(virtualenv_name)$ deactivate
- Install Dependencies
# install django to your v.env
pipenv install django
# to use mysql you have to install mysql
pipenv install mysql
# also install this
pip install mysql-python
# install mysql-connector
pip install mysql-connector
# if above doesn't work use this
pip install mysql-connector-python
- Create a django app
django-admin startproject dcrm
- Create a app named website
python manage.py startapp website
- Install
autopep8
to auto format Python code.
pip install -U autopep8
- Create a
mydb.py
file to create a database.
import mysql.connector
dataBase=mysql.connector.connect(
host=<your_hostname>,
user=<database_username>,
passwd=<database_password>
)
#prepare cursor object
cursorObject=dataBase.cursor()
# create database
cursorObject.execute("CREATE DATABASE <DATABASE_NAME>")
print ("All Done")
- Execute this file using
python mydb.py
. - Then create a superuser to login to django site as admin.
winpty python manage.py createsuperuser
- Make changes to your site and run your server
python manage.py runserver
.
- After creating a table you have to migrate it to make changes in your server.
python manage.py makemigrations
python manage.py migrate
Deploying A Django Site to Python Anywhere
You can deploy only one free website as a begineer in Python Anywhere.
- Login or Signup.
- Then you should be able to access the
Dashboard
.here.Make sure you have a github repository of your project to deploy.
- In the dashboard you should see a console tab. Click on
$ bash
, it will open a console.
On the console type pwd
to make sure you are here /home/<your_username>
pwd
- Then clone your github repository
git clone https://github.com/<your_github_username>/<your_Reposity_name>
- In pythonAnywhere, We need to create a virtual environment.
# use any python version >=3.10
mkvirtualenv --python=/usr/bin/python3.9
- Then install your dependencies.
# insatll django
pip install django
# syntax
pip install <dependency_name>
- Open
Web
Tab.
- Click on add a new web app.
- Then click next.
- Then Select
Manual configuration
in python web framework. click next. - Select
Python Version
. We selected3.9
. click next Manual Configuration
.Click next.- [Web App setup is done]
- Then Scroll Down in
Web
tab in your app.
- Navigate to
Virtualenv
. - There add your
virtual environment name
.
- Then Scroll Up to
Code
.
- Click on
WSGI configuration file
. - It will open a editor.
- Edit and save the file with following code.
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/kalyanvelu/django-crm/mysite/settings.py'
## and your manage.py is is at '/home/kalyanvelu/django-crm/manage.py'
path = '/home/kalyanvelu/django-crm'
if path not in sys.path:
sys.path.append(path)
# also change this from mysite.setting to your folder where your setting exist
os.environ['DJANGO_SETTINGS_MODULE'] = 'dcrm.settings'
#
## then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
- you have to get the path of your application from your bash console in Python Anywhere