From 26859412eef3e1271289b32106f0d06be5353429 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Tue, 19 Sep 2023 23:50:35 +0700 Subject: [PATCH 1/6] Update README.md Layout --- README.md | 98 +++++++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 19d4982..3f16273 100644 --- a/README.md +++ b/README.md @@ -4,87 +4,48 @@ An application to conduct online polls and surveys based on the [Django Tutorial project](https://docs.djangoproject.com/en/4.2/intro/tutorial01/), with additional features. -## Install and Run -### Run Setup.py (Recommended) -1. Install [Python 3.11 or later](https://www.python.org/downloads/) -2. Clone this repository and Run `setup.py` to install and run the project +## Installation + +Here is **[Install Instruction](Installation.md)**. + +## How to Run + +### Setup.py Method + +Using setup.py method, it will set `.env` for you and runserver automatically with [--insecure](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/#cmdoption-runserver-insecure). +You can set `DEBUG=True` later and then runserver normally to load static files. + +Then, connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) + +### Manual Installation +After follow all of instruction. +- Set Virtual Environment and Use it. +- Install Dependencies. +- Migrate and then Load fixtures. +- Set variable of `.env` + +You can run with this command. -**Don't forget to answer the question from `setup.py` to setup the project** ```bash -git clone https://github.com/Sosokker/ku-polls -cd ku-polls -python setup.py -``` -If you want to customize the environment variables, name of environment folder then run this command -```bash -python setup.py -custom -``` -or run `setup.ps1` (For Windows User) - ----- - -### Manual Installation (If the above method doesn't work) -1. Install [Python 3.11 or later](https://www.python.org/downloads/) -2. Run these commands to clone and install requirements.txt -```bash -git clone https://github.com/Sosokker/ku-polls -cd ku-polls -pip install -r requirements.txt -``` -3. Create file call `.env` in `ku-polls` directory and add this line -```bash -SECRET_KEY=your_secret_key -``` - -You can generate your own `your_secret_key` by this command -```bash -python manage.py shell -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())" -``` -or -- [Django Secret Key Generator #1](https://djecrety.ir/) -- [Django Secret Key Generator #2](https://miniwebtool.com/django-secret-key-generator/) - -**Don't forget to change `your_secret_key` to your secret key (without quote)** - -**You can look at `sample.env` for more information and others environment variables to set.** - -4. Run these commands -```bash -python manage.py migrate -python manage.py loaddata data/users.json -python manage.py loaddata data/polls.json python manage.py runserver ``` -***NOTE*** +Anyway, if you set `DEBUG = False` then django production server will not load static files for you. +You need to set `DEBUG = True` or run this command. -By Default `DEBUG=False` and Django will not load Static files for you so if you want to apply CSS run this. ```bash python manage.py runserver --insecure ``` -or set `DEBUG=True` -or do the [collectstatic](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/) -Then connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) or [localhost:8000/](localhost:8000/) +Then, connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) ----- +--- -**Recommend** - -You can create virtual environment by using this command before install requirements.txt - -1. Install virtualenv via pip +**NOTE** : If you have problems with port you can use runserver and specific your own port. +For example, If you run this command then you need to connect to [http://127.0.0.1:7000/](http://127.0.0.1:7000/) ```bash -python -m pip install --user virtualenv -``` -2. Run these commands -```bash -python -m virtualenv .venv -``` -3. Use `virtual environment` -```bash -.venv\Scripts\activate +python manage.py runserver 7000 ``` ## Demo Superuser @@ -112,5 +73,6 @@ All project documents are in the [Project Wiki](https://github.com/Sosokker/ku-p - [Iteration1](https://github.com/Sosokker/ku-polls/wiki/Iteration-1-Plan) - [Iteration2](https://github.com/Sosokker/ku-polls/wiki/Iteration-2-Plan) - [Iteration3](https://github.com/Sosokker/ku-polls/wiki/Iteration-3-Plan) +- [Iteration4](https://github.com/Sosokker/ku-polls/wiki/Iteration-4-Plan) -[django-tutorial](https://docs.djangoproject.com/en/4.2/intro/tutorial01/) +[Django-Tutorial](https://docs.djangoproject.com/en/4.2/intro/tutorial01/) From a7f4451fc932a255f06fcc3441f31834d82373c2 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Tue, 19 Sep 2023 23:51:54 +0700 Subject: [PATCH 2/6] Update Installation.md --- Installation.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Installation.md b/Installation.md index da70d2e..054b207 100644 --- a/Installation.md +++ b/Installation.md @@ -82,6 +82,7 @@ or python manage.py migrate python manage.py loaddata data/users.json python manage.py loaddata data/polls.json +python manage.py loaddata data/vote.json python manage.py runserver ``` From c8d35735a60ffbbb501ef34dcd680dfee49f71cb Mon Sep 17 00:00:00 2001 From: sosokker Date: Thu, 21 Sep 2023 20:34:04 +0700 Subject: [PATCH 3/6] no activate in venv when install with setup.py in posix --- setup.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 7b871ff..c9bb97c 100644 --- a/setup.py +++ b/setup.py @@ -20,13 +20,20 @@ def check_python_command(): return None +def install_virtualenv(python_commands): + # Set check=True to throw an error if the return code is non-zero (which is an indication of some error happening). + try: + subprocess.run([python_commands, "-m", "pip", "install", "--user", "virtualenv"], check=True) + except: + print("+++Error Occur when try to install Virtualenv+++") + def create_virtual_environment(env_name, python_command): - subprocess.run([python_command, "-m", "venv", env_name], check=True) + subprocess.run([python_command, "-m", "virtualenv", env_name], check=True) def customize_virtual_environment(): env_name = input("Enter a custom virtual environment name (or press Enter for the default): ").strip() if not env_name: - env_name = ".venv" + env_name = "venv" return env_name def setup_environment_variables(python_command_in_venv): @@ -105,19 +112,21 @@ def main(): setup_venv = input("Do you want to set up a virtual environment? (yes/no): ").lower() if setup_venv == "yes": print("==========================Default Mode==========================") - if not os.path.exists(".venv"): - print("Creating a new virtual environment...") - subprocess.run([python_command, "-m", "venv", ".venv"]) + print("==========================Install Virtualenv==========================") + install_virtualenv(python_command) + if not os.path.exists("venv"): + print("==========================Creating a new virtual environment...==========================") + subprocess.run([python_command, "-m", "virtualenv", "venv"]) else: print("==========================Using an existing virtual environment.==========================") if is_posix: - activate_command = os.path.join(".venv", "bin", "activate") + activate_command = os.path.join("venv", "bin", "activate") elif is_windows: - activate_command = os.path.join(".venv", "Scripts", "activate") + activate_command = os.path.join("venv", "Scripts", "activate") subprocess.run([activate_command], shell=True) - python_command = os.path.join(".venv", "bin", "python") if is_posix else os.path.join(".venv", "Scripts", "python") + python_command = os.path.join("venv", "bin", "python") if is_posix else os.path.join("venv", "Scripts", "python") else: print("Not setting up a virtual environment. Using the global Python interpreter.") @@ -149,6 +158,8 @@ def main(): print("==========================Custom Mode==========================") python_commands = check_python_command() env_name = customize_virtual_environment() + print("==========================Install Virtualenv==========================") + install_virtualenv(python_commands) create_virtual_environment(env_name, python_commands) print(f"Finishing Create Virtual environment {env_name}") python_command_in_venv = os.path.join(env_name, "bin", "python") if is_posix else os.path.join(env_name, "Scripts", "python") From fc784a2ce56beeecb513a6d8574d1189184db456 Mon Sep 17 00:00:00 2001 From: sosokker Date: Thu, 21 Sep 2023 20:48:47 +0700 Subject: [PATCH 4/6] Update Installation Guide and .env --- Installation.md | 14 +++++++++----- sample.env | 6 +++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Installation.md b/Installation.md index 054b207..3b63363 100644 --- a/Installation.md +++ b/Installation.md @@ -31,7 +31,7 @@ or run `setup.ps1` (For Windows User) 2. Run these commands to clone this repository and enter the directory. ```bash git clone https://github.com/Sosokker/ku-polls -cd ku +cd ku-polls ``` 3. (Optional: You can use venv instead)Install virtualenv via pip @@ -41,20 +41,20 @@ python -m pip install --user virtualenv ``` 4. Create virtual environment with `venv` or `virtualenv`. ```bash -python -m virtualenv .venv +python -m virtualenv venv or -python -m venv .venv +python -m venv venv ``` 5. Use `virtual environment` - Windows ```bash -.venv\Scripts\activate +.\venv\Scripts\activate ``` - Linux or MacOS ```bash -source .venv/bin/activate +source venv/bin/activate ``` 6. Install require module. ``` @@ -65,6 +65,10 @@ pip install -r requirements.txt **You can look at `sample.env` for more information and others environment variables to set.** ```bash SECRET_KEY=your_secret_key +DEBUG = False +ALLOWED_HOSTS = *.ku.th, localhost, 127.0.0.1, ::1 +TIME_ZONE = Asia/Bangkok +EMAIL_HOST_PASSWORD = yourpassword ``` You can generate your own `your_secret_key` by this command diff --git a/sample.env b/sample.env index c5af6b1..4c2f812 100644 --- a/sample.env +++ b/sample.env @@ -7,4 +7,8 @@ DEBUG = False # You can use wildcard chars (*) and IP addresses. Use * for any host. ALLOWED_HOSTS = *.ku.th, localhost, 127.0.0.1, ::1 # Your timezone -TIME_ZONE = Asia/Bangkok \ No newline at end of file +TIME_ZONE = Asia/Bangkok +# Password to use for the SMTP server defined in EMAIL_HOST. This setting is used in conjunction +# with EMAIL_HOST_USER when authenticating to the SMTP server. +# If either of these settings is empty, Django won’t attempt authentication. +EMAIL_HOST_PASSWORD=somepassword \ No newline at end of file From e53dc578cd805687c4fb9fe98217e6a591473e97 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Thu, 21 Sep 2023 20:53:45 +0700 Subject: [PATCH 5/6] Update django.yml - Add 3.10, change to 3.11 test --- .github/workflows/django.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 8b103a8..976a566 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -13,7 +13,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.11.4] + python-version: [3.10, 3.11] steps: - uses: actions/checkout@v3 From c1c796d9d8a1061eed38f2a9367caba7a2f264ad Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Thu, 21 Sep 2023 20:55:16 +0700 Subject: [PATCH 6/6] Update django.yml - Remove 3.10 --- .github/workflows/django.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 976a566..ad737c9 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -13,7 +13,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.10, 3.11] + python-version: [3.11] steps: - uses: actions/checkout@v3