modelo home
models.py
from django.db import models
# Create your models here.
class Prueba(models.Model):
titulo = models.CharField(max_length=100)
subtitulo = models.CharField(max_length=50)
cantidad = models.IntegerField()
def __str__(self):
return self.titulo + '-' + self.subtitulo
views.py
from django.shortcuts import render
#
from django.views.generic import (
TemplateView,
ListView,
CreateView
)
# import models
from .models import Prueba
from .forms import PruebaForm
class PruebaCreateView(CreateView):
template_name = "home/add.html"
model = Prueba
form_class = PruebaForm
success_url = '/'
forms.py
from django import forms
from .models import Prueba
class PruebaForm(forms.ModelForm):
class Meta:
model = Prueba
fields = (
'titulo',
'subtitulo',
'cantidad',
)
widgets = {
'titulo': forms.TextInput(
attrs = {
'placeholder': 'Ingrese texto aqui',
}
)
}
# 'clean' siempre debe ir y tambien el 'self'
def clean_cantidad(self):
cantidad = self.cleaned_data['cantidad'] #recupera el valor del atributo 'cantidad' del modelo Prueba
if cantidad < 10:
raise forms.ValidationError('Ingrese un numero mayor a 10') #
return cantidad
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path(
'add/',
views.PruebaCreateView.as_view(),
name='prueba_add',
),
]
templates/persona/add.html
<h2>Probando creaetview</h2>
<form method="POST">{% csrf_token %}
{{form.as_p}}
<button type="submit">Guardar</button>
</form>

Última modificación: 05 Aug 2023 20:14
No hay comentarios aún
Sé el primero en comentar este artículo