xxxxxxxxxx
duplicate key value violates unique constraint "loan_loanapplication_pkey"
DETAIL: Key (id)=(1) already exists.
xxxxxxxxxx
pip install django # Install Django Packages
django-admin startproject project_name # Create a Project
cd project_name
python manage.py makemigrations # Create new migrations
python manage.py migrate # Apply Migrations
python manage.py createsuperuser # Create User for admin
python manage.py runserver # Start Server
python manage.py startapp sub_module # Create a sub app
xxxxxxxxxx
## Django Basics
### Setting Up a Virtual Environment
- **Create a new virtual environment**:
```bash
mkvirtualenv env
```
- **Activate the virtual environment** (works from any directory):
```bash
workon env
## IF virtualenvs are outside of folder somewhere
cd ~/.virtualenvs
```
Alternatively, if not using `mkvirtualenv`:
```bash
source env/Scripts/activate # For Windows
source env/bin/activate # For macOS/Linux
```
- **Deactivate the virtual environment**:
```bash
deactivate
```
### Starting a Django Project
1. **Start a new Django project**:
```bash
django-admin startproject project_name
```
2. **Navigate to the project directory**:
```bash
cd project_name
```
### Running the Django Development Server
- **Start the server**:
```bash
python manage.py runserver
```
- Access the app in your browser at:
`http://127.0.0.1:8000/`
- **Specify a custom port (e.g., 8080)**:
```bash
python manage.py runserver 8080
```
### Managing Migrations
- **Create migration files for your app**:
```bash
python manage.py makemigrations
```
- **Apply the migrations to the database**:
```bash
python manage.py migrate
```
- **Rollback a migration**:
```bash
python manage.py migrate app_name migration_name
```
(Example: `python manage.py migrate app_name 0001`)
- **View the current migration status**:
```bash
python manage.py showmigrations
```
### Creating a New App
- **Create an app named `vege`**:
```bash
python manage.py startapp vege
```
- **Register the app in `INSTALLED_APPS`** (found in `settings.py`):
```python
INSTALLED_APPS = [
...,
'vege',
]
```
### Admin Panel
- **Create a superuser** (admin user):
```bash
python manage.py createsuperuser
```
- Provide username, email, and password when prompted.
- Access the admin panel at:
`http://127.0.0.1:8000/admin/`
### Database Commands
- **Open the Django database shell**:
```bash
python manage.py dbshell
```
- **Reset the database (delete and recreate migrations)**:
```bash
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
python manage.py makemigrations
python manage.py migrate
```
### Other Useful Commands
- **Run the Django shell**:
```bash
python manage.py shell
```
- **Collect static files** (for production):
```bash
python manage.py collectstatic
```
- **Check for project issues**:
```bash
python manage.py check
```
### Tips
1. Always activate your virtual environment before running Django commands.
2. Use version control (e.g., Git) to manage your project.
3. For larger projects, consider using a more robust database like PostgreSQL or MySQL instead of SQLite.
Start building with ChatGPT (OpenAI)
xxxxxxxxxx
git clone https://github.com/openai/openai-quickstart-node.git
npm install
npm run dev
Read more https://beta.openai.com/docs/quickstart/introduction
xxxxxxxxxx
# Start a Project.
django-admin startproject <project name>
# cd into <project name>
# Create the application
python manage.py startapp <app name>
# Start using the files.