15๐
โ
You define the form
variable in this if request.method == 'POST':
block.
If you access the view
with a GET request form
gets not defined.
You should change the view to something like this:
def contato(request):
form_class = ContactForm
# if request is not post, initialize an empty form
form = form_class(request.POST or None)
if request.method == 'POST':
if form.is_valid():
nome = request.POST.get('nome')
email = request.POST.get('email')
msg = request.POST.get('msg')
send_mail('Subject here', msg, email, ['testmail@gmail.com'], fail_silently=False)
return HttpResponseRedirect('blog/inicio')
return render(request, 'blog/inicio.html', {'form': form})
๐คilse2005
3๐
The Django docs handle this, but slightly differently from the other answers. See https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view
Using an else, if the request is not POST then create the blank form. The below is pasted directly from the docs.
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
๐คalexbhandari
- Django admin โ how to get all registered models in templatetag?
- How to measure the time profile of each django test?
- How to call asynchronous function in Django?
0๐
If there can be so to say โ collateral damage by initializing an empty form
, then you may also provide a dummy value as a filler , till the request.POST
is initialized โฆ
def contato(request):
form = "Dummy String"
form_class = ContactForm
# if request is not post, initialize an empty form
#form = form_class(request.POST or None) # Maybe Not
form = form_class(request.POST) # Instead
if request.method == 'POST':
if form.is_valid():
nome = request.POST.get('nome')
email = request.POST.get('email')
msg = request.POST.get('msg')
send_mail('Subject here', msg, email, ['testmail@gmail.com'], fail_silently=False)
return HttpResponseRedirect('blog/inicio')
return render(request, 'blog/inicio.html', {'form': form})
๐คRohit Dhankar
- Django: Implementing a referral program
- Quieting pylint false-positives when using django
- Run django application without django.contrib.admin
0๐
The only issue you have is you did not declare ContactForm as function,
change FROM this;
def contato(request):
form_class = ContactForm
if request.method == 'POST':
TO;
def contato(request):
form_class = ContactForm()
if request.method == 'POST':
๐คnasir dev
- Django โ how can I access the form field from inside a custom widget
- How to configure Apache to run ASGI in Django Channels? Is Apache even required?
- Add dynamic field to django admin model form
Source:stackexchange.com