This project is a Django backend setup with PostgreSQL for user authentication, including admin roles. It provides endpoints for user registration, login, profile management, and admin user management.
- Python 3.x
- PostgreSQL
- Virtualenv
- Thunder Client (VS Code extension) or any other API testing tool
git clone <repository_url>
cd <repository_name>python -m venv myenvFor Windows:
myenv\Scripts\activateFor macOS/Linux:
source myenv/bin/activatepip install django psycopg2-binary djangorestframework djangorestframework-simplejwt-
Install PostgreSQL: Download and install PostgreSQL from the official website.
-
Create a Database and User:
- Open pgAdmin and create a new database (e.g.,
myprojectdb). - Create a new user (e.g.,
myprojectuser) and set a password. - Grant all privileges to the user for the database.
- Open pgAdmin and create a new database (e.g.,
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
python manage.py makemigrations
python manage.py migratepython manage.py createsuperuserpython manage.py runserver- URL:
http://127.0.0.1:8000/api/register/ - Method:
POST - Body:
{
"email": "user@example.com",
"name": "User",
"phone_number": "1234567890",
"employee_id": "EMP001",
"profile_pic": null,
"password": "password"
}- URL:
http://127.0.0.1:8000/api/token/ - Method:
POST - Body:
{ "email": "user@example.com", "password": "password" } - Response:
{
"refresh": "your_refresh_token",
"access": "your_access_token"
}- URL:
http://127.0.0.1:8000/api/profile/ - Method:
GET - Headers:
Authorization: Bearer your_access_token
- URL:
http://127.0.0.1:8000/api/admin/users/ - Method:
GET - Headers:
Authorization: Bearer your_access_token
### 5. Admin Update or Delete User
- **URL**: `http://127.0.0.1:8000/api/admin/users/<user_id>/`
- **Method**: `PUT` or `DELETE`
- **Headers**:
``` plaintext
Authorization: Bearer your_access_token
- Body (for PUT):
{
"email": "user@example.com",
"name": "Updated User",
"phone_number": "0987654321",
"employee_id": "EMP002",
"profile_pic": null
}-
Install Thunder Client: Install the Thunder Client extension from the VS Code Extensions Marketplace.
-
Obtain Token:
- Create a new request with the following details:
- Method:
POST - URL:
http://127.0.0.1:8000/api/token/ - Body:
{ "email": "user@example.com", "password": "password" } - Method:
- Send the request and copy the
accesstoken from the response.
- Create a new request with the following details:
-
Access Profile:
- Create a new request with the following details:
- Method:
GET - URL:
http://127.0.0.1:8000/api/profile/ - Headers:
Authorization: Bearer your_access_token
- Method:
- Send the request to access the profile data.
- Create a new request with the following details:
By following these steps, you can set up, run, and test your Django backend with user authentication and admin roles. If you encounter any issues, please refer to the Django documentation or seek further assistance.