# 072-012-fale-co-nosco-form-email

## Dica 12 - Fale conosco com formulário para enviar mensagem

[![](/files/-MfuAP1e7W4erW9hf6Ic)](https://youtu.be/XXSAalrmtxI)

**Importante:** remova a `\` no meio das tags.

![](/files/5yQAAuqmpHdOp0r1qaxa)

Criar app `crm`

```
cd backend
python ../manage.py startapp crm
cd ..

touch backend/crm/forms.py
touch backend/crm/urls.py
```

Edite `crm/apps.py`

```python
# crm/apps.py
from django.apps import AppConfig


class CrmConfig(AppConfig):
    ...
    name = 'backend.crm'
```

Edite `settings.py`

```python
# settings.py
INSTALLED_APPS = [
    ...
    # minhas apps
    'backend.core',
    'backend.crm',
]
```

Edite `backend/urls.py`

```python
# backend/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    ...
    path('crm/', include('backend.crm.urls', namespace='crm')),  # noqa E501
    ...
]
```

Edite `crm/urls.py`

```python
# crm/urls.py
from django.urls import path

from backend.crm import views as v

app_name = 'crm'


urlpatterns = [
    path('contact/', v.send_contact, name='send_contact'),  # noqa E501
]
```

Edite `crm/forms.py`

```python
# crm/forms.py
from django import forms


class ContactForm(forms.Form):
    name = forms.CharField('nome', max_length=255)
    email = forms.EmailField('email',)  # sender
    title = forms.CharField('título', max_length=100)  # subject
    body = forms.CharField(widget=forms.Textarea)  # message
```

Edite `crm/views.py`

## crm/views.py

```python
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.views.decorators.http import require_http_methods

from .forms import ContactForm


@require_http_methods(['POST'])
def send_contact(request):
    form = ContactForm(request.POST or None)

    if form.is_valid():
        subject = form.cleaned_data.get('title')
        message = form.cleaned_data.get('body')
        sender = form.cleaned_data.get('email')
        send_mail(
            subject,
            message,
            sender,
            ['localhost'],
            fail_silently=False,
        )
        return redirect('core:index')
```

Edite `index.html`

```html
<form action="{\% url 'crm:send_contact' %}" method="POST">
```


---

# 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://www.dicas-de-django.com.br/072-012-fale-co-nosco-form-email.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.
