Skip to content

Commit 66914dc

Browse files
committed
Primer commit: proyecto Django + Docker + Postgres
0 parents  commit 66914dc

22 files changed

Lines changed: 2131 additions & 0 deletions

.dockerignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo
4+
*.pyd
5+
.Python
6+
env/
7+
venv/
8+
pip-log.txt
9+
pip-delete-this-directory.txt
10+
.tox/
11+
.coverage
12+
.coverage.*
13+
.cache
14+
nosetests.xml
15+
coverage.xml
16+
*.cover
17+
*.log
18+
.git
19+
.mypy_cache
20+
.pytest_cache
21+
.hypothesis
22+
*.sqlite3
23+
db.sqlite3
24+
*.db
25+
.DS_Store
26+
*.swp
27+
*.swo
28+
*~
29+
.idea/
30+
.vscode/

Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM python:3.11-slim
2+
3+
# Variables de entorno para Python
4+
ENV PYTHONDONTWRITEBYTECODE=1
5+
ENV PYTHONUNBUFFERED=1
6+
7+
# Directorio de trabajo
8+
WORKDIR /app
9+
10+
# Instalar dependencias del sistema necesarias para PostgreSQL
11+
RUN apt-get update && apt-get install -y \
12+
postgresql-client \
13+
gcc \
14+
python3-dev \
15+
musl-dev \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# Copiar archivo de dependencias
19+
COPY requirements.txt /app/
20+
21+
# Actualizar pip e instalar dependencias Python
22+
RUN pip install --upgrade pip && \
23+
pip install --no-cache-dir -r requirements.txt
24+
25+
# Copiar todo el proyecto
26+
COPY . /app/
27+
28+
# Exponer puerto 8000
29+
EXPOSE 8000
30+
31+
# Comando por defecto (se sobrescribe en docker-compose)
32+
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
version: '3.8'
2+
3+
services:
4+
# Servicio de Base de Datos PostgreSQL
5+
db:
6+
image: postgres:15-alpine
7+
container_name: django_postgres
8+
volumes:
9+
- postgres_data:/var/lib/postgresql/data
10+
environment:
11+
POSTGRES_DB: django_db
12+
POSTGRES_USER: django_user
13+
POSTGRES_PASSWORD: django_pass
14+
ports:
15+
- "5432:5432"
16+
healthcheck:
17+
test: ["CMD-SHELL", "pg_isready -U django_user -d django_db"]
18+
interval: 5s
19+
timeout: 5s
20+
retries: 5
21+
networks:
22+
- django_network
23+
24+
# Servicio de Django
25+
web:
26+
build: .
27+
container_name: django_web
28+
command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
29+
volumes:
30+
- .:/app
31+
ports:
32+
- "8000:8000"
33+
environment:
34+
DB_NAME: django_db
35+
DB_USER: django_user
36+
DB_PASSWORD: django_pass
37+
DB_HOST: db
38+
DB_PORT: 5432
39+
DJANGO_SETTINGS_MODULE: myproject.settings
40+
depends_on:
41+
db:
42+
condition: service_healthy
43+
networks:
44+
- django_network
45+
46+
volumes:
47+
postgres_data:
48+
49+
networks:
50+
django_network:
51+
driver: bridge

init.bat

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@echo off
2+
echo Iniciando proyecto Django con Docker...
3+
4+
echo Construyendo imagenes Docker...
5+
docker-compose build
6+
7+
echo Verificando si manage.py existe...
8+
if not exist "manage.py" (
9+
echo Creando proyecto Django...
10+
docker-compose run --rm web django-admin startproject myproject .
11+
) else (
12+
echo Proyecto Django ya existe
13+
)
14+
15+
echo Verificando si app users existe...
16+
if not exist "users\" (
17+
echo Creando app de usuarios...
18+
docker-compose run --rm web python manage.py startapp users
19+
) else (
20+
echo App users ya existe
21+
)
22+
23+
echo.
24+
echo Estructura base creada!
25+
echo.
26+
echo Proximos pasos:
27+
echo 1. Configura settings.py segun la guia
28+
echo 2. Crea los modelos, serializers y vistas
29+
echo 3. Ejecuta: docker-compose up
30+
pause

manage.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

myproject/__init__.py

Whitespace-only changes.

myproject/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for myproject project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
15+
16+
application = get_asgi_application()

myproject/settings.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import os
2+
from pathlib import Path
3+
4+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
5+
BASE_DIR = Path(__file__).resolve().parent.parent
6+
7+
8+
# Quick-start development settings - unsuitable for production
9+
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
10+
11+
# SECURITY WARNING: keep the secret key used in production secret!
12+
SECRET_KEY = 'django-insecure-5pef)xgr-#oy-eh*e(n&l8x0!g)ik&uu8jgvo*zcz7uz3nw#@e'
13+
14+
# SECURITY WARNING: don't run with debug turned on in production!
15+
DEBUG = True
16+
17+
ALLOWED_HOSTS = []
18+
19+
20+
# Application definition
21+
22+
INSTALLED_APPS = [
23+
'django.contrib.admin',
24+
'django.contrib.auth',
25+
'django.contrib.contenttypes',
26+
'django.contrib.sessions',
27+
'django.contrib.messages',
28+
'django.contrib.staticfiles',
29+
# Third party
30+
'rest_framework',
31+
# Apps locales
32+
'users',
33+
]
34+
35+
MIDDLEWARE = [
36+
'django.middleware.security.SecurityMiddleware',
37+
'django.contrib.sessions.middleware.SessionMiddleware',
38+
'django.middleware.common.CommonMiddleware',
39+
'django.middleware.csrf.CsrfViewMiddleware',
40+
'django.contrib.auth.middleware.AuthenticationMiddleware',
41+
'django.contrib.messages.middleware.MessageMiddleware',
42+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
43+
]
44+
45+
ROOT_URLCONF = 'myproject.urls'
46+
47+
TEMPLATES = [
48+
{
49+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
50+
'DIRS': [],
51+
'APP_DIRS': True,
52+
'OPTIONS': {
53+
'context_processors': [
54+
'django.template.context_processors.debug',
55+
'django.template.context_processors.request',
56+
'django.contrib.auth.context_processors.auth',
57+
'django.contrib.messages.context_processors.messages',
58+
],
59+
},
60+
},
61+
]
62+
63+
WSGI_APPLICATION = 'myproject.wsgi.application'
64+
65+
66+
# Database
67+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
68+
69+
DATABASES = {
70+
'default': {
71+
'ENGINE': 'django.db.backends.postgresql',
72+
'NAME': os.environ.get('DB_NAME', 'django_db'),
73+
'USER': os.environ.get('DB_USER', 'django_user'),
74+
'PASSWORD': os.environ.get('DB_PASSWORD', 'django_pass'),
75+
'HOST': os.environ.get('DB_HOST', 'localhost'),
76+
'PORT': os.environ.get('DB_PORT', '5432'),
77+
}
78+
}
79+
80+
81+
# Password validation
82+
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
83+
84+
AUTH_PASSWORD_VALIDATORS = [
85+
{
86+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
87+
},
88+
{
89+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
90+
},
91+
{
92+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
93+
},
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
96+
},
97+
]
98+
99+
100+
# Internationalization
101+
# https://docs.djangoproject.com/en/4.2/topics/i18n/
102+
103+
104+
# Internacionalización
105+
LANGUAGE_CODE = 'es-pe'
106+
TIME_ZONE = 'America/Lima'
107+
USE_I18N = True
108+
USE_TZ = True
109+
110+
111+
# Static files (CSS, JavaScript, Images)
112+
# https://docs.djangoproject.com/en/4.2/howto/static-files/
113+
114+
# Archivos estáticos
115+
STATIC_URL = 'static/'
116+
117+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
118+
119+
# 🔥 MODELO DE USUARIO PERSONALIZADO
120+
AUTH_USER_MODEL = 'users.CustomUser'
121+
122+
# 🔥 CONFIGURACIÓN DE REST FRAMEWORK
123+
REST_FRAMEWORK = {
124+
'DEFAULT_AUTHENTICATION_CLASSES': [
125+
'rest_framework.authentication.SessionAuthentication',
126+
],
127+
'DEFAULT_PERMISSION_CLASSES': [
128+
'rest_framework.permissions.IsAuthenticated',
129+
],
130+
}

myproject/urls.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
from django.contrib import admin
3+
from django.urls import path, include
4+
5+
urlpatterns = [
6+
path('admin/', admin.site.urls),
7+
path('api/', include('users.urls')),
8+
]

myproject/wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for myproject project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
15+
16+
application = get_wsgi_application()

0 commit comments

Comments
 (0)