Modelos:
persona.py
#
from model_utils.models import TimeStampedModel
#
from django.db import models
#
class Hobby(TimeStampedModel):
hobby = models.CharField(
'Pasatiempo',
max_length=50,
)
class Meta:
verbose_name='Hobby'
verbose_name_plural='Hobbies'
def __str__(self):
return self.hobby
#
class Person(TimeStampedModel):
""" Modelo para registrar personas de una agenda """
full_name = models.CharField(
'Nombres',
max_length=50,
)
job = models.CharField(
'Trabajo',
max_length=30,
blank=True
)
email = models.EmailField(
blank=True,
null=True
)
phone = models.CharField(
'telefono',
max_length=15,
blank=True,
)
hobbies = models.ManyToManyField(Hobby)
class Meta:
verbose_name = 'Persona'
verbose_name_plural = 'Personas'
def __str__(self):
return self.full_name
class Reunion(TimeStampedModel):
""" Modelo para reunion """
persona = models.ForeignKey(
Person,
on_delete=models.CASCADE
)
fecha = models.DateField()
hora = models.TimeField()
asunto = models.CharField(
'Aunto de Reunion',
max_length=100
)
class Meta:
verbose_name = 'Reunion'
verbose_name_plural = 'Reunion'
def __str__(self):
return self.asunto
views.py
from django.shortcuts import render
from django.views.generic import ListView,TemplateView
#
from rest_framework.generics import (
ListAPIView,
CreateAPIView,
RetrieveAPIView, #Detailview
DestroyAPIView,
UpdateAPIView,
RetrieveUpdateAPIView,
)
urls.py
#
from django.urls import path,re_path
#
from .import views
app_name='persona_app'
urlpatterns = [
path(
)
]
Archivos de Configuracion
base.py
from django.core.exceptions import ImproperlyConfigured
import json
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from unipath import Path
BASE_DIR = Path(__file__).ancestor(3)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
with open("secret.json") as f:
secret = json.loads(f.read())
def get_secret(secret_name, secrets=secret):
try:
return secrets[secret_name]
except:
msg = "la variable %s no existe" % secret_name
raise ImproperlyConfigured(msg)
SECRET_KEY = get_secret('SECRET_KEY')
# Application definition
DJANGO_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
LOCAL_APPS = (
'applications.home',
'applications.persona',
)
THIRD_PARTY_APPS = (
'rest_framework',
)
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'agenda.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR.child('templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'agenda.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
local.py
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': get_secret('DB_NAME'),
'USER': get_secret('USER'),
'PASSWORD': get_secret('PASSWORD'),
'HOST': 'localhost',
'PORT': '5432',
}
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR.child('static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR.child('media')
urls.py
"""agenda URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path, include
urlpatterns = [
path('admin/', admin.site.urls),
re_path('', include('applications.persona.urls')),
]
Última modificación: 20 Dec 2022 21:36
No hay comentarios aún
Sé el primero en comentar este artículo