Curso Django. Formularios I. Vídeo 21
Introduction to Working with Forms in Django
In this section, we will learn about working with forms in Django. Forms are essential elements in dynamic websites and are used for various purposes such as blog comments, online shopping carts, and contact forms. We will start by understanding the basic concept of sending data from a form to the server using an HTTP request object.
Creating a Form Manually
- A form is sent to the server using an HTTP request object.
- The request object has methods and properties that allow the server to manipulate and obtain valuable information about the user, such as IP address, browser details, etc.
- It is recommended to refer to the official Django documentation for detailed information on the HTTP request object.
- We will create a simple HTML form manually within our project's templates folder.
- Create a new file named "busqueda_productos.html" inside the "templates" folder.
- Build the basic structure of the HTML form with appropriate tags like
<form>,<input>, and<button>.
- Assign names to input fields so that we can retrieve user-entered data later.
Registering URL and Creating a View
- We need to register the URL for our form and create a view function that renders it.
- Open the "views.py" file and create a function-based view, such as "busqueda_productos".
- The view function should accept a request parameter.
- We can use the render() function to render the HTML form template.
Conclusion
Desarrollo de Formularios y Vistas en Django
En esta sección, se muestra cómo desarrollar formularios y vistas en Django para permitir la búsqueda de productos.
Renderizando el formulario
- Utilizar la función
renderpara devolver el renderizado del formulario.
- El primer parámetro de
renderes la petición (request).
- El segundo parámetro de
renderes el formulario creado.
- Registrar una URL adicional en el archivo
urls.pypara dirigirse al formulario.
Creando la vista de búsqueda
- Importar las vistas necesarias.
- Crear una nueva vista llamada "buscar" que reciba la petición como parámetro.
- Utilizar el método
getdel objetorequestpara obtener la información enviada desde el cuadro de texto del formulario.
- Devolver un objeto
HttpResponsecon la información obtenida.
Gestión de Búsqueda en Django
En esta sección, se muestra cómo gestionar la información enviada desde un formulario de búsqueda en Django.
Corrigiendo errores
- Cambiar el tipo del botón del formulario a "submit" en lugar de "button".
- Registrar la vista "buscar" en el archivo
urls.py.
Implementando la lógica de búsqueda
- Crear una variable llamada "mensaje" que contendrá el artículo buscado.
- Utilizar el método
getdel objetorequestpara obtener la información enviada desde el cuadro de texto del formulario.
- Devolver un objeto
HttpResponsecon el mensaje y los datos obtenidos.
Finalización y Recursos Adicionales
En esta sección final, se proporciona información adicional y recursos para continuar aprendiendo sobre Django.
- Se invita a los espectadores a registrarse en la academia virtual del instructor.
- Se mencionan los cursos gratuitos disponibles en la plataforma.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.