guias update ia
This commit is contained in:
+132
-121
@@ -1,207 +1,218 @@
|
||||
AYUDA DJANGO
|
||||
# Guía de Django
|
||||
|
||||
\============
|
||||
Esta guía explica cómo crear un proyecto Django sencillo con una aplicación de blog.
|
||||
|
||||
Proyecto Django: Blog Simple
|
||||
|
||||
Este proyecto creará un blog básico donde los usuarios podrán ver una lista de publicaciones y acceder a los detalles de cada una.
|
||||
|
||||
Paso 1: Instalar Django
|
||||
|
||||
Si no tienes Django instalado, ejecuta:
|
||||
## 1. Instalar Django
|
||||
|
||||
```bash
|
||||
pip install django
|
||||
```
|
||||
|
||||
Paso 2: Crear un Proyecto Django
|
||||
---
|
||||
|
||||
django-admin startproject blog\_project
|
||||
## 2. Crear un proyecto
|
||||
|
||||
cd blog\_project
|
||||
```bash
|
||||
django-admin startproject blog_project
|
||||
cd blog_project
|
||||
```
|
||||
|
||||
Paso 3: Crear la Aplicación del Blog
|
||||
---
|
||||
|
||||
## 3. Crear la aplicación
|
||||
|
||||
```bash
|
||||
python manage.py startapp blog
|
||||
```
|
||||
|
||||
Registre la aplicación en blog\_project/settings.pydentro de INSTALLED\_APPS:
|
||||
|
||||
INSTALLED\_APPS = [
|
||||
|
||||
'django.contrib.admin',
|
||||
|
||||
'django.contrib.auth',
|
||||
|
||||
'django.contrib.contenttypes',
|
||||
|
||||
'django.contrib.sessions',
|
||||
|
||||
'django.contrib.messages',
|
||||
|
||||
'django.contrib.staticfiles',
|
||||
|
||||
'blog', # Agregamos nuestra aplicación
|
||||
Registra la aplicación en `blog_project/settings.py`:
|
||||
|
||||
```python
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'blog',
|
||||
]
|
||||
```
|
||||
|
||||
Paso 4: Definir el modelo de publicación
|
||||
---
|
||||
|
||||
Edita blog/models.py:
|
||||
## 4. Definir el modelo
|
||||
|
||||
Edita `blog/models.py`:
|
||||
|
||||
```python
|
||||
from django.db import models
|
||||
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=200)
|
||||
content = models.TextField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
title = models.CharField(max\_length=200)
|
||||
def __str__(self):
|
||||
return self.title
|
||||
```
|
||||
|
||||
content = models.TextField()
|
||||
|
||||
created\_at = models.DateTimeField(auto\_now\_add=True)
|
||||
|
||||
def \_\_str\_\_(self):
|
||||
|
||||
return self.title
|
||||
|
||||
Aplica las migraciones para crear la tabla en la base de datos:
|
||||
Genera las migraciones:
|
||||
|
||||
```bash
|
||||
python manage.py makemigrations
|
||||
|
||||
python manage.py migrate
|
||||
```
|
||||
|
||||
Paso 5: Registrar el Modelo en el Panel de Administración
|
||||
---
|
||||
|
||||
Edita blog/admin.py:
|
||||
## 5. Registrar el modelo en el admin
|
||||
|
||||
Edita `blog/admin.py`:
|
||||
|
||||
```python
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Post
|
||||
|
||||
admin.site.register(Post)
|
||||
```
|
||||
|
||||
Crea un superusuario:
|
||||
|
||||
```bash
|
||||
python manage.py createsuperuser
|
||||
```
|
||||
|
||||
Corre el servidor y accede al panel de administración en http://127.0.0.1:8000/admin:
|
||||
Ejecuta el proyecto:
|
||||
|
||||
```bash
|
||||
python manage.py runserver
|
||||
```
|
||||
|
||||
Desde ahí, puedes agregar publicaciones manualmente.
|
||||
Accede a:
|
||||
|
||||
Paso 6: Crear las Vistas y URL
|
||||
```text
|
||||
http://127.0.0.1:8000/admin
|
||||
```
|
||||
|
||||
Edita blog/views.py:
|
||||
---
|
||||
|
||||
from django.shortcuts import render, get\_object\_or\_404
|
||||
## 6. Crear vistas y rutas
|
||||
|
||||
Edita `blog/views.py`:
|
||||
|
||||
```python
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from .models import Post
|
||||
|
||||
def lista\_posts(request):
|
||||
|
||||
posts = Post.objects.all().order\_by('-created\_at')
|
||||
def lista_posts(request):
|
||||
posts = Post.objects.all().order_by('-created_at')
|
||||
return render(request, 'blog/lista_posts.html', {'posts': posts})
|
||||
|
||||
return render(request, 'blog/lista\_posts.html', {'posts': posts})
|
||||
|
||||
def detalle\_post(request, post\_id):
|
||||
def detalle_post(request, post_id):
|
||||
post = get_object_or_404(Post, id=post_id)
|
||||
return render(request, 'blog/detalle_post.html', {'post': post})
|
||||
```
|
||||
|
||||
post = get\_object\_or\_404(Post, id=post\_id)
|
||||
|
||||
return render(request, 'blog/detalle\_post.html', {'post': post})
|
||||
|
||||
Crea blog/urls.py:
|
||||
Crea `blog/urls.py`:
|
||||
|
||||
```python
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
path('', views.lista\_posts, name='lista\_posts'),
|
||||
|
||||
path('post/<int:post\_id>/', views.detalle\_post, name='detalle\_post'),
|
||||
|
||||
path('', views.lista_posts, name='lista_posts'),
|
||||
path('post/<int:post_id>/', views.detalle_post, name='detalle_post'),
|
||||
]
|
||||
```
|
||||
|
||||
Registra las rutas en blog\_project/urls.py:
|
||||
Registra las URLs en `blog_project/urls.py`:
|
||||
|
||||
```python
|
||||
from django.contrib import admin
|
||||
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
path('admin/', admin.site.urls),
|
||||
|
||||
path('', include('blog.urls')),
|
||||
|
||||
path('admin/', admin.site.urls),
|
||||
path('', include('blog.urls')),
|
||||
]
|
||||
```
|
||||
|
||||
Paso 7: Crear las Plantillas HTML
|
||||
---
|
||||
|
||||
Dentro de blog/templates/blog/, crea lista\_posts.html:
|
||||
## 7. Crear plantillas HTML
|
||||
|
||||
Crea la siguiente estructura:
|
||||
|
||||
```text
|
||||
blog/templates/blog/
|
||||
```
|
||||
|
||||
### `lista_posts.html`
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
|
||||
<title>Blog</title>
|
||||
|
||||
<title>Blog</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<h1>Publicaciones</h1>
|
||||
|
||||
<ul>
|
||||
|
||||
{% for post in posts %}
|
||||
|
||||
<li>
|
||||
|
||||
<a href="{% url 'detalle\_post' post.id %}">{{ post.title }}</a>
|
||||
|
||||
({{ post.created\_at|date:"d M Y" }})
|
||||
|
||||
</li>
|
||||
|
||||
{% endfor %}
|
||||
|
||||
</ul>
|
||||
|
||||
<h1>Publicaciones</h1>
|
||||
<ul>
|
||||
{% for post in posts %}
|
||||
<li>
|
||||
<a href="{% url 'detalle_post' post.id %}">{{ post.title }}</a>
|
||||
({{ post.created_at|date:"d M Y" }})
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
```
|
||||
|
||||
Crea detalle\_post.html:
|
||||
### `detalle_post.html`
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
|
||||
<title>{{ post.title }}</title>
|
||||
|
||||
<title>{{ post.title }}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<h1>{{ post.title }}</h1>
|
||||
|
||||
<p>{{ post.created\_at|date:"d M Y" }}</p>
|
||||
|
||||
<p>{{ post.content }}</p>
|
||||
|
||||
<a href="{% url 'lista\_posts' %}">Volver al blog</a>
|
||||
|
||||
<h1>{{ post.title }}</h1>
|
||||
<p>{{ post.created_at|date:"d M Y" }}</p>
|
||||
<p>{{ post.content }}</p>
|
||||
<a href="{% url 'lista_posts' %}">Volver al blog</a>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
```
|
||||
|
||||
Paso 8: Ejecutar el Proyecto
|
||||
---
|
||||
|
||||
Corre el servidor:
|
||||
## 8. Ejecutar el proyecto
|
||||
|
||||
```bash
|
||||
python manage.py runserver
|
||||
```
|
||||
|
||||
Abre http://127.0.0.1:8000/y prueba la aplicación.
|
||||
Abre la aplicación en:
|
||||
|
||||
```text
|
||||
http://127.0.0.1:8000/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. Resumen
|
||||
|
||||
Con Django puedes crear rápidamente:
|
||||
|
||||
- proyectos
|
||||
- aplicaciones reutilizables
|
||||
- modelos de base de datos
|
||||
- vistas y rutas
|
||||
- plantillas HTML
|
||||
|
||||
Si quieres, puedo continuar con una guía avanzada de Django: CRUD, autenticación, administración, formularios y despliegue.
|
||||
|
||||
Reference in New Issue
Block a user