From ed8adcae712f9accbafcfda1b08bac90640adc13 Mon Sep 17 00:00:00 2001 From: sina lalehbakhsh Date: Sat, 6 Jan 2024 12:43:54 -0500 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=92=A0=20121-Django/Practice=5F01-30/P?= =?UTF-8?q?ractice-13=20Online=20Shop=20Image-05=20Environment=20Variables?= =?UTF-8?q?=201-=20write=20in=20a=20directory=20Terminal=202-=20Create=20p?= =?UTF-8?q?rivate/public=20repository=20in=20your=20account=20github=203-?= =?UTF-8?q?=20in=20Terminal=20->=20echo=20'#=20OnlineShope'=20>>=20README.?= =?UTF-8?q?md=204-=20in=20Terminal=20->=20git=20init=205-=20in=20Terminal?= =?UTF-8?q?=20->=20git=20add=20README.md=206-=20=20in=20Terminal=20->=20gi?= =?UTF-8?q?t=20commit=20-m=20'first=20commit'=207-=20=20in=20Terminal=20->?= =?UTF-8?q?=20git=20branch=20-M=20main=208-=20=20in=20Terminal=20->=20git?= =?UTF-8?q?=20remote=20add=20origin=20git@github.com:=E2=9B=94YOUR=5FUSER?= =?UTF-8?q?=5FNAME=E2=9B=94/OnlineShope.git=209-=20=20in=20Terminal=20->?= =?UTF-8?q?=20git=20push=20-u=20origin=20main=2010-=20Check=20your=20repos?= =?UTF-8?q?itory=20link,=20make=20sure=20that=20will=20changed=2011-=20pas?= =?UTF-8?q?te=20everything=20from=20directory=20you=20cloned=20in=20step?= =?UTF-8?q?=201=20from=20this=20link=20https://github.com/hadiMh/Django=5F?= =?UTF-8?q?Docker=5Fwith=5FPostgreSql=2012-=20paste=20this=20file=20from?= =?UTF-8?q?=20this=20link=20in=20U.sh=2013-=20change=20permission=20U.sh?= =?UTF-8?q?=20by=20this=20command=20in=20Terminal=2014-=20Check=20permissi?= =?UTF-8?q?on=20file=20by=20this=20command=20in=20Terminal=2015-=20run=20t?= =?UTF-8?q?his=20for=20first=20time=20use=20docker-compose=2016-=20write?= =?UTF-8?q?=20in=20Terminal=20->=20docker-compose=20up=20--build=2017-=20R?= =?UTF-8?q?un=20strong=20VPN=20and=20write=20again=20in=20Terminal=2018-?= =?UTF-8?q?=20write=20Ctrl=20+=20C=20key=20and=20shut=20it=20down=2019-=20?= =?UTF-8?q?Run=20again=20(strong=20VPN=20activated)=2020-=20Check=20in=20b?= =?UTF-8?q?rowser=20should=20see=20default=20django=20page=20=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0?= =?UTF-8?q?=F0=9F=92=A0=20BEFORE=20migrate=20=E2=9B=94=E2=9B=94=E2=9B=94?= =?UTF-8?q?=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94?= =?UTF-8?q?=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94?= =?UTF-8?q?=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94?= =?UTF-8?q?=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94=E2=9B=94?= =?UTF-8?q?=E2=9B=94=E2=9B=94=20If=20for=20any=20reason=20you=20have=20to?= =?UTF-8?q?=20rebuild=20and=20delete=20containers=20web=20+=20databas=20Yo?= =?UTF-8?q?u=20need=20run=20again=20these=20command=20before=20to=20contin?= =?UTF-8?q?ue=20docker-compose=20up=20--build=20docker-compose=20exec=20we?= =?UTF-8?q?b=20python=20manage.py=20makemigrations=20docker-compose=20exec?= =?UTF-8?q?=20web=20python=20manage.py=20migrate=20docker-compose=20exec?= =?UTF-8?q?=20web=20python=20manage.py=20createsuperuser=2021-=20in=20Term?= =?UTF-8?q?inal=20->=20docker-compose=20exec=20web=20python=20manage.py=20?= =?UTF-8?q?startapp=20accounts=2022-=20add=20accounts=20in=20config/settin?= =?UTF-8?q?gs.py=20>=20INSTALLED=5FAPPS=20list=2023-first=20docker=20rm=20?= =?UTF-8?q?containers=20and=20second=20rmi=20image=2024-=20create=20fake?= =?UTF-8?q?=20venv,=20write=20in=20Terminal=2025-=20Create=20.gitignore=20?= =?UTF-8?q?file=20and=20add=20this=20into=20it=2026-=20import=20this=20int?= =?UTF-8?q?o=20accounts/models.py=2027-=20create=20new=20class=20in=20acco?= =?UTF-8?q?unts/models.py=2028-=20Create=20forms.py=20in=20accounts=20dire?= =?UTF-8?q?ctory=2029-=20import=20this=20in=20accounts/forms.py=2030-=20Cr?= =?UTF-8?q?eate=20new=20class=20in=20accounts/forms.py=2031-=20add=20this?= =?UTF-8?q?=20in=20config/settings.py=2032-=20Create=20new=20class=20in=20?= =?UTF-8?q?accounts/forms.py=2033-=20import=20this=20into=20accounts/admin?= =?UTF-8?q?.py=2034-=20create=20new=20class=20in=20accounts/admin.py=2035-?= =?UTF-8?q?=20in=20Terminal=20->=20docker-compose=20exec=20web=20python=20?= =?UTF-8?q?manage.py=20makemigrations=2036-=20in=20Terminal=20->=20docker-?= =?UTF-8?q?compose=20exec=20web=20python=20manage.py=20migrate=2037-=20in?= =?UTF-8?q?=20Terminal=20->=20docker-compose=20exec=20web=20python=20manag?= =?UTF-8?q?e.py=20createsuperuser=2038-=20in=20Terminal=20->=20docker-comp?= =?UTF-8?q?ose=20up=20--build=20=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=20For?= =?UTF-8?q?=20All=20Projects=20=F0=9F=92=A0=F0=9F=92=A0=F0=9F=92=A0=20=20?= =?UTF-8?q?=20=20=201-=20in=20Terminal=20=20=20=20=20docker-compose=20up?= =?UTF-8?q?=20--build=20=20=20=20=20=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=20=20=20=20=202-?= =?UTF-8?q?=20in=20Terminal=20=20=20=20=20Ctrl=20+=20C=20key=20=20=20=20?= =?UTF-8?q?=20=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=20=20=20=20=203-=20in=20Terminal=20=20=20?= =?UTF-8?q?=20=20docker=20start=202=5Fdjango=5Fhaji=5Fweb=5F1=202=5Fdjango?= =?UTF-8?q?=5Fhaji=5Fdb=5F1=20=20=20=20=20OR=20=20=20=20=20docker=20start?= =?UTF-8?q?=20web=5FName=20dataBase=5FName=20=20=20=20=20=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=20=20=20=20=204-=20in=20Terminal=20=20=20=20=20docker-compo?= =?UTF-8?q?se=20exec=20web=20python=20manage.py=20makemigrations=20=20=20?= =?UTF-8?q?=20=20=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=20=20=20=20=205-=20in=20Terminal=20=20?= =?UTF-8?q?=20=20=20docker-compose=20exec=20web=20python=20manage.py=20mig?= =?UTF-8?q?rate=20=20=20=20=20=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=20=20=20=20=206-=20in?= =?UTF-8?q?=20Terminal=20=20=20=20=20docker-compose=20up=20--build=20=20?= =?UTF-8?q?=20=20=20=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=20=20=20=20=207-=20in=20another=20T?= =?UTF-8?q?erminal=20=20=20=20=20docker-compose=20exec=20web=20python=20ma?= =?UTF-8?q?nage.py=20createsuperuser=20=20=20=20=20=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=20?= =?UTF-8?q?=20=20=20=208-=20in=20browser=20=20=20=20=20http://127.0.0.1:80?= =?UTF-8?q?00/admin/login/=3Fnext=3D/admin/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Output, it must be implemented correctly 39- in Terminal -> docker-compose exec web python manage.py startapp pages 40- add pages in config/settings.py > INSTALLED_APPS list -> 'pages', 41- import this into pages/views.py -> from django.views.generic import TemplateView 42- create new two classes into pages/views.py 43- Create tempaltes/home.html in current directory of project -> tempaltes/home.html 44- write in tempaltes/home.html -> Home Page is Here Dear YOUR_NAME :) 45- write this in config/settings.py TEMPLATES list -> 'DIRS': [str(BASE_DIR.joinpath('templates'))], 46- Rebuild Server -> docker-compose up --build 47- Create tempaltes/pages/aboutus.html in pages directory -> templates/pages/aboutus.html 48- Write in tempaltes/pages/aboutus.html -> About Us YOU from Earth 49- import this in config/urls.py -> from django.urls import include 50- write into config/urls.py > urlpatterns list -> path('', include('pages.urls')), 51- Create urls.py in pages directory -> urls.py 52- import in pages/urls.py 53- Create urlpatterns list in pages/urls.py 54- Rebuild server in Terminal -> docker-compose up --build 55- Remove previous images maybe are like this IDs 56- Check browser 57- write this in config/settings.py in urlpatterns list 58- Create urls.py in accounts directory 59- import into accounts/urls.py 60- Create urlpatterns list in accounts/urls.py 61- import into accounts/views.py 62- Create new class into accounts/views.py 63- Create registration/login.html AND registration/signup.html in templates current directory project 64- write these in templates/registration/signup.html 65- write these in templates/registration/login.html 66- Rebuild Server -> docker-compose up 67- Check browser -> http://127.0.0.1:8000/admin/ 68- Check this link -> http://127.0.0.1:8000/accounts/login/ 69- Check this link -> http://127.0.0.1:8000/accounts/logout/ 70- Check this link -> http://127.0.0.1:8000/accounts/signup/ 71- write this in config/settings.py 72- make sure server is true running like this 73- Check these links should be redirected after login/logout 🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑🥑 You Can write TEST for everything in this step in test.py file Apps 74- in another Terminal (Server should be acticeted now) -> docker-compose exec web pip install django-crispy-forms 75- Check requirements.txt must be like this 76- in another Terminal (Server should be acticeted now) -> docker-compose exec web pip freeze > requirements.txt 77- Check requirements.txt must be Changed like this 78- write this into config/settings.py in INSTALLED_APPS > after Djangop default Apps 79- for convinience way to after this step, write this in Terminal -> pipenv install 80- for running any command for (docker-compose exec web ...) in terminal 🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲🧲 From now on, execute Docker-compose commands inside this terminal, when the pipenv environment is not active. 81- Create _base.html in templates directory -> _base.html 82- write html:5 in file _base.html and Press Enter(return) 83- search in browser 'bootstrap' 84- Copy 'Bootstrap CSS' from this link -> https://getbootstrap.com/docs/5.3/getting-started/introduction/ 85- Paste that in head tag of _base.html 86- Copy - - -================================================ -87- Paste that end of body tag in _base.html -... - - - - - -... -================================================ -88- Change title of _base.html like this -{% block page_title %}{% endblock %} -================================================ -89- inside body tag of _base.html add this - - {% block content %} - {% endblock %} - ... -================================================ -90- change inside home.html like this -{% extends '_base.html' %} -{% block page_title %} - Home -{% endblock %} -{% block content %} - Home Page is Here Dear Sina :) -{% endblock %} -================================================ -91- Copy all of home.html -Paste on login.html -================================================ -92- Change login.html like these -{% extends '_base.html' %} -{% block page_title %} - Login -{% endblock %} -{% block content %} -
- {% csrf_token %} - {{ form.as_p }} - -
-{% endblock %} -================================================ -93- Copy all login.html -Paste on signup.html -================================================ -94- Change signup.html like these -{% extends '_base.html' %} -{% block page_title %} - Signup -{% endblock %} -{% block content %} -
- {% csrf_token %} - {{ form.as_p }} - -
-{% endblock %} -================================================ -95- Check site in broswer all fonts should be changed -http://127.0.0.1:8000/ -================================================ -96- in end of config/settings.py -# crispy forms settings -CRISPY_TEMPLATE_PACK = 'bootstrap' -================================================ -97- add this in after {extends} login.html like this -{% extends '_base.html' %} -{% load crispy_forms_tags %} -... -================================================ -98- Change this line in login.html like this -... -{{ form|crispy }} -... -================================================ -99- Apply the previous two steps to the signup.html page -... -{% load crispy_forms_tags %} -... -{{ form|crispy }} -... -================================================ -100- Check these -100-1 Check this link for dependencies of crispy_forms: - https://django-crispy-forms.readthedocs.io/en/latest/install.html#template-packs - -100-2 Install crispy-bootstrap4 from this link - https://pypi.org/project/crispy-bootstrap4/ - 1- in another Terminal vscode - 2- don't activate pipenv or another environments - for docker command is like this: - docker-compose exec web pip install crispy-bootstrap4 - Output it may look like this code: - Collecting crispy-bootstrap4 - Downloading crispy_bootstrap4-2023.1-py3-none-any.whl (22 kB) - Requirement already satisfied: django>=4.2 in /usr/local/lib/python3.9/site-packages (from crispy-bootstrap4) (4.2.8) - Requirement already satisfied: django-crispy-forms>=2.0 in /usr/local/lib/python3.9/site-packages (from crispy-bootstrap4) (2.1) - Requirement already satisfied: asgiref<4,>=3.6.0 in /usr/local/lib/python3.9/site-packages (from django>=4.2->crispy-bootstrap4) (3.7.2) - Requirement already satisfied: sqlparse>=0.3.1 in /usr/local/lib/python3.9/site-packages (from django>=4.2->crispy-bootstrap4) (0.4.2) - Requirement already satisfied: typing-extensions>=4 in /usr/local/lib/python3.9/site-packages (from asgiref<4,>=3.6.0->django>=4.2->crispy-bootstrap4) (4.9.0) - Installing collected packages: crispy-bootstrap4 - Successfully installed crispy-bootstrap4-2023.1 - WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv - - [notice] A new release of pip is available: 23.0.1 -> 23.3.2 - [notice] To update, run: pip install --upgrade pip -================================================ -101- add this in config/settings.py INSTALLED_APPS list -... -# Third Parties -'crispy_forms', -"crispy_bootstrap4", -... -================================================ -102- add this in config/settings.py like this -... -# crispy forms settings -CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4" -CRISPY_TEMPLATE_PACK = 'bootstrap4' -================================================ -103- Check these links should be changed -1- -http://127.0.0.1:8000/accounts/login/ -2- -http://127.0.0.1:8000/accounts/signup/ -================================================ -103- -================================================ -================================================ -================================================ -================================================ -================================================ -Rebuild Server -docker-compose up --build -Output should be like this: diff --git a/U.sh b/U.sh index 84739fa..96f808f 100755 --- a/U.sh +++ b/U.sh @@ -1,6 +1,6 @@ #!/bin/bash git add . -git commit -m " 💠 121-Django/Practice_01-30/Practice-13 Online Shop Image-04 Django All-Auth +git commit -m " 💠 121-Django/Practice_01-30/Practice-13 Online Shop Image-05 Environment Variables 1- write in a directory Terminal 2- Create private/public repository in your account github 3- in Terminal -> echo '# OnlineShope' >> README.md @@ -189,6 +189,17 @@ Every package you add means you are adding a vulnerability to your site. 141- in broswer go to admin panel -> http://127.0.0.1:8000/admin/ 142- in broswer go to users -> http://127.0.0.1:8000/admin/accounts/customuser/ 143- Check account Username , You created in step 133 , Should be like this +144- Install this in another Terminal (Docker should be running right now) +145- add previous step in requirements.txt by this command in Terminal +146- write this in config/settings.py after imports -> from environs import Env +147- install again for pipenv, write in Terminal -> pipenv install -r requirements.txt +148- write these in config/settings.py, after step 146 +149- Cut the values of SECRET_KEY without these signs '' like this +150- change value of SECRET_KEY like this -> SECRET_KEY = env('DJANGO_SECRET_KEY') +151- create new dependency in docker-compose.yml file after ports:... +152- create new environment in docker-compose.yml file after depends_on:... +153- If in values of SECRET_KEY was a $ sign repeat $ again after it like this -> django-insecure-b764$$#(&o4o((cvy!z^nsseq5@erq#e^0%5r5a(%c24*f^*sde +154- Rebuild Server -> docker-compose up --build " git push -u origin main diff --git a/config/settings.py b/config/settings.py index bf2ded3..c14c9e0 100644 --- a/config/settings.py +++ b/config/settings.py @@ -11,6 +11,11 @@ """ from pathlib import Path +from environs import Env + +# For Environment Variables +env = Env() +env.read_env() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -20,7 +25,7 @@ # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-b764$#(&o4o((cvy!z^nsseq5@erq#e^0%5r5a(%c24*f^*sde' +SECRET_KEY = env('DJANGO_SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True diff --git a/docker-compose.yml b/docker-compose.yml index b31a227..4808206 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,10 @@ services: - .:/code ports: - 8000:8000 + depends_on: + - db + environment: + - 'DJANGO_SECRET_KEY=django-insecure-b764$$#(&o4o((cvy!z^nsseq5@erq#e^0%5r5a(%c24*f^*sde' db: image: postgres:14 diff --git a/requirements.txt b/requirements.txt index 1c9ff79..1ee81e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,14 +5,21 @@ charset-normalizer==3.3.2 crispy-bootstrap4==2023.1 cryptography==41.0.7 defusedxml==0.7.1 +dj-database-url==2.1.0 +dj-email-url==1.0.6 Django==4.2.8 django-allauth==0.59.0 +django-cache-url==3.4.5 django-crispy-forms==2.1 +environs==10.0.0 idna==3.6 +marshmallow==3.20.1 oauthlib==3.2.2 +packaging==23.2 psycopg2-binary==2.9.3 pycparser==2.21 PyJWT==2.8.0 +python-dotenv==1.0.0 python3-openid==3.2.0 requests==2.31.0 requests-oauthlib==1.3.1