You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: es/extend_your_application/README.md
+43-50Lines changed: 43 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,26 +11,26 @@ Ya tenemos un modelo `Post`, así que no necesitamos añadir nada a `models.py`.
11
11
## Crea un enlace en la plantilla
12
12
13
13
Vamos a empezar añadiendo un enlace dentro del archivo `blog/templates/blog/post_list.html`. Hasta el momento debería verse así:
14
-
```html
15
-
{% extends 'blog/base.html' %}
16
-
17
-
{% block content %}
18
-
{% for post in posts %}
19
-
<divclass="post">
20
-
<divclass="date">
21
-
{{ post.published_date }}
22
-
</div>
23
-
<h1><ahref="">{{ post.title }}</a></h1>
24
-
<p>{{ post.text|linebreaksbr }}</p>
14
+
```html
15
+
{% extends 'blog/base.html' %}
16
+
17
+
{% block content %}
18
+
{% for post in posts %}
19
+
<divclass="post">
20
+
<divclass="date">
21
+
{{ post.published_date }}
25
22
</div>
26
-
{% endfor %}
27
-
{% endblock %}
23
+
<h1><ahref="">{{ post.title }}</a></h1>
24
+
<p>{{ post.text|linebreaksbr }}</p>
25
+
</div>
26
+
{% endfor %}
27
+
{% endblock %}
28
28
```
29
29
30
30
Queremos tener un enlace a una página de detalle sobre el título del post. Vamos a cambiar `<h1><a href="">{{ post.title }}</a></h1>` dentro del enlace:
Es hora de explicar el misterioso `{% url 'post_detail' pk=post.pk %}`. Como probablemente sospeches, la notación `{% %}` significa que estamos utilizando Django template tags. ¡Esta vez vamos a utilizar uno que va a crear una dirección URL para nosotros!
36
36
@@ -48,7 +48,7 @@ Ahora cuando vayamos a: http://127.0.0.1:8000/ tendremos un error (como era de e
48
48
49
49
Queremos crear una URL que apunte a Django a una *view* denominada `post_detail`, que mostrará una entrada del blog. Agrega la línea `url (r'^post/(?P<pk>[0-9]+)/$', views.post_detail),` al archivo `blog/urls.py`. Debería tener este aspecto:
50
50
51
-
```python
51
+
```python
52
52
from django.conf.urls import url
53
53
from . import views
54
54
@@ -84,10 +84,8 @@ Esta vez nuestra *view* tomará un parámetro adicional `pk`. Nuestra *view* nec
84
84
85
85
Ahora, queremos sólo un post del blog. Para ello podemos usar querysets como este:
86
86
87
-
```python
88
-
89
-
Post.objects.get(pk=pk)
90
-
87
+
```python
88
+
Post.objects.get(pk=pk)
91
89
```
92
90
93
91
Pero este código tiene un problema. Si no hay ningún `Post` con `llave primaria` (`pk`) tendremos un error muy feo.
@@ -109,19 +107,16 @@ La buena noticia es que puedes crear tu propia página `Page Not Found` y diseñ
109
107
Deberíamos abrir `blog/views.py` y agregar el siguiente código:
110
108
111
109
```python
112
-
113
-
from django.shortcuts import render, get_object_or_404
114
-
110
+
from django.shortcuts import render, get_object_or_404
115
111
```
116
112
117
113
Cerca de otras líneas `from`. Y en el final del archivo añadimos nuestra *view*:
Sí. Es hora de actualizar la página: http://127.0.0.1:8000/
127
122
@@ -142,19 +137,19 @@ Crearemos un archivo en `blog/templates/blog` llamado `post_detail.html`.
142
137
Se verá así:
143
138
144
139
```html
145
-
{% extends 'blog/base.html' %}
146
-
147
-
{% block content %}
148
-
<divclass="post">
149
-
{% if post.published_date %}
150
-
<divclass="date">
151
-
{{ post.published_date }}
152
-
</div>
153
-
{% endif %}
154
-
<h1>{{ post.title }}</h1>
155
-
<p>{{ post.text|linebreaksbr }}</p>
156
-
</div>
157
-
{% endblock %}
140
+
{% extends 'blog/base.html' %}
141
+
142
+
{% block content %}
143
+
<divclass="post">
144
+
{% if post.published_date %}
145
+
<divclass="date">
146
+
{{ post.published_date }}
147
+
</div>
148
+
{% endif %}
149
+
<h1>{{ post.title }}</h1>
150
+
<p>{{ post.text|linebreaksbr }}</p>
151
+
</div>
152
+
{% endblock %}
158
153
```
159
154
160
155
Una vez más estamos extendiendo `base.html`. En el bloque `content` queremos mostrar la fecha de publicación (si existe), título y texto de nuestros posts. Pero deberíamos discutir algunas cosas importantes, ¿cierto?
@@ -174,20 +169,18 @@ Bien, podemos actualizar nuestra página y ver si `Page Not Found` se ha ido.
174
169
Sería bueno verificar que tu sitio web aún funcionará en PythonAnywhere, ¿cierto? Intentemos desplegar de nuevo.
175
170
176
171
```
177
-
178
-
$ git status
179
-
$ git add --all .
180
-
$ git status
181
-
$ git commit -m "Added views to create/edit blog post inside the site."
182
-
$ git push
183
-
184
-
```
172
+
$ git status
173
+
$ git add --all .
174
+
$ git status
175
+
$ git commit -m "Added views to create/edit blog post inside the site."
176
+
$ git push
177
+
```
185
178
186
179
* Luego, en una [consola Bash de PythonAnywhere][8]
187
180
188
181
```
189
-
$ cd my-first-blog
190
-
$ git pull [...]
182
+
$ cd my-first-blog
183
+
$ git pull [...]
191
184
```
192
185
193
186
* Finalmente, ve a la pestaña [Web][9] y haz click en **Reload**.
0 commit comments