219 lines
3.6 KiB
Markdown
219 lines
3.6 KiB
Markdown
# Guía de Django
|
|
|
|
Esta guía explica cómo crear un proyecto Django sencillo con una aplicación de blog.
|
|
|
|
## 1. Instalar Django
|
|
|
|
```bash
|
|
pip install django
|
|
```
|
|
|
|
---
|
|
|
|
## 2. Crear un proyecto
|
|
|
|
```bash
|
|
django-admin startproject blog_project
|
|
cd blog_project
|
|
```
|
|
|
|
---
|
|
|
|
## 3. Crear la aplicación
|
|
|
|
```bash
|
|
python manage.py startapp blog
|
|
```
|
|
|
|
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',
|
|
]
|
|
```
|
|
|
|
---
|
|
|
|
## 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)
|
|
|
|
def __str__(self):
|
|
return self.title
|
|
```
|
|
|
|
Genera las migraciones:
|
|
|
|
```bash
|
|
python manage.py makemigrations
|
|
python manage.py migrate
|
|
```
|
|
|
|
---
|
|
|
|
## 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
|
|
```
|
|
|
|
Ejecuta el proyecto:
|
|
|
|
```bash
|
|
python manage.py runserver
|
|
```
|
|
|
|
Accede a:
|
|
|
|
```text
|
|
http://127.0.0.1:8000/admin
|
|
```
|
|
|
|
---
|
|
|
|
## 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')
|
|
return render(request, 'blog/lista_posts.html', {'posts': posts})
|
|
|
|
|
|
def detalle_post(request, post_id):
|
|
post = get_object_or_404(Post, id=post_id)
|
|
return render(request, 'blog/detalle_post.html', {'post': post})
|
|
```
|
|
|
|
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'),
|
|
]
|
|
```
|
|
|
|
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')),
|
|
]
|
|
```
|
|
|
|
---
|
|
|
|
## 7. Crear plantillas HTML
|
|
|
|
Crea la siguiente estructura:
|
|
|
|
```text
|
|
blog/templates/blog/
|
|
```
|
|
|
|
### `lista_posts.html`
|
|
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<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>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
### `detalle_post.html`
|
|
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<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>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
---
|
|
|
|
## 8. Ejecutar el proyecto
|
|
|
|
```bash
|
|
python manage.py runserver
|
|
```
|
|
|
|
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.
|