-
Create environment: terminal
pipenv --python 3.7
pipenv shell
enter the virtual environment -
That will create Pipfile file containing: ''' [[source]] name = "pypi" url = "https://pypi.org/simple" verify_ssl = true
[dev-packages] [packages] [requires] python_version = "3.7" '''
-
add django to Pipfile under [requires]: django = "==2.2.0"
-
pipenv install
that install the required packaches -
Create django project:
django-admin startproject whatsonme
-
???????? not worked until:
pipenv install django
-
migrate database the basic:
python manage.py migrate
-
run the server:
python manage.py runserver
-
Create the app: "auth_app"
python manage.py startapp auth_api
installpsycopg2
for database bindings to PostgreSQL
#####Put the project on Docker: 10. Change to postgres db: 11. change the settings to use the postgres: ''' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'HOST': 'db', # will take from docker-compose.yml 'PORT': 5432 } } '''
....etc . . . . . . . . . .
- run the docker file from the directory containes the Dockerfile:
docker-compose up --build
it will reach to the point of running the server and run 2 containers: - whatsonme_web_1 - whatsonme_db_1 - to create a superuser:
-stop the containers:
docker-compose down
- create the superuser:
docker-compose run web python /code/manage.py createsuperuser
fill... then:docker-compose up --build
will auto migrate and run the server
- create the superuser:
if not available on the system:
brew install make
the available orders:
-
make start
start the container -
make stop
stop the container -
make restart
restart the container -
make superuser
create the superuser for the project -
make db
connect to the database psql
- Serializer: transform the data into JSON
- add the
djangorestframework
to the Pipfile to install it. the problem is the Pipfile will install it but didn't update the Pipfile.lock to solve this but i think it is not the right way: insert this line in Dockerfile to install djangorestframework then the .lock file will be updatedRUN pipenv install djangorestframework
- add
rest_framework
to theINSTALLED_APPS
in settings.py - create
serializers.py
file inside the app (auth_api) - using Django RESTful Framework generic views instead of custom views and template because it is just API and they are pre-built views
- update
auth_api/views.py
to include generic views and create list_view and details_view - the project's url point to the auth_api path
- update the app url
- the API supports CRUD
- The path will be /users/home, /users/login
- modify the project urls.py file to include
path('users/', include(django.contrib.auth.urls))
- create template folder and inside of it registration folder that will including the teplates for the users view urls
- the login.html template will include the login template
{{ form.as_p }}
- add the path to the templates folder in the setttings file
- add
LOGIN_REDIRECT_URL = '/'
to redirect the success login to - create basic, home pages to login logout and modify the login template that will extend the base page
- in the url.py we will use the generic.base template and connect our home page with it
- now we need to create view and url so we create an app for that (users)
- create a new app (users)
docker-compose exec web /usr/local/bin/python3 manage.py startapp users
- add urls.py to the app
- include the app in the settings installed apps
- point to the app in the project urls.py add the path above the built in path that created for login/out that it will go the first path appeared (because they have the same path name 'users/')
- in the views:
''' Using reverse_lazy instead of reverse? The reason is that for all generic class-based views the urls are not loaded when the file is imported, so we have to use the lazy form of reverse to load them later when they’re available. ''' - create the template for signup (signup.html)
- to signup go to
http://0.0.0.0:8000/users/signup/
- save sent mails to file temporarly (future by mail settings) set
EMAIL_BACKEND
andEMAIL_FILE_PATH
- the path is
users/password_reset
- create templates:
password_reset_form.html
password_reset_done.html
password_reset_confirm.html
password_reset_complete.html
- put the built in url in the home page:
{% url 'password_reset' %}
- when request restart a file created inside the folder sent_email containing the email that will be sent including the link to reset the password. it works
###Cleanup
- delete migration files
- Drop the database
- in the mode create a class
User(AbstractUser)
- change the default user to custome user in settings:
AUTH_USER_MODEL = 'users.User'
- create signup form (SignUpForm) in the app