# URLs e Views

## URL

Vamos abrir o arquivo `blog/urls.py` e escrever:

blog/urls.py

```python
path(r'^post/new/$', views.post_new, name='post_new'),
```

O código final deve se parecer com isso:

blog/urls.py

```python
from django.urls import path
from blog import views

urlpatterns = [
    path('', views.post_list),
    path(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
    path(r'^post/new/$', views.post_new, name='post_new'),
]
```

Após atualizar o site, nós veremos um `AttributeError`, já que nós não ainda temos a view `post_new` implementada. Vamos adicioná-la agora.

## A view post\_new

Hora de abrir o arquivo `blog/views.py` e adicionar as linhas seguintes com o resto das linhas `from`:

blog/views.py

```python
from .forms import PostForm
```

e então a nossa *view*:

blog/views.py

```python
def post_new(request):
    form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})
```

Para criar um novo formulário `Post`, nós devemos chamar `PostForm()` e passá-lo para o template. Nós voltaremos para esta *view*, mas por agora, vamos criar rapidamente um template para o formulário.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dgportoalegre.gitbook.io/djangogirls/urls-e-views.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
