我正在获取SyntaxError:在我的代码上“返回”外部函数。 我已经检查了缩进,没关系,有人可以帮我吗? 这是我的代码。
from django.shortcuts import render
from .forms import form
from django.core.mail import send_mail
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
return render(request, 'index.html', {})
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['name']
message = form.cleaned_data['name']
sender = form.cleaned_data['sender']
cc_myself = form.cleaned_data['cc_myself']
recipients = ['hillariouskelly@gmail.com']
if cc_myself:
recipients.append(sender)
send_mail(name, email, message, sender, recipients)
return HttpResponseRedirect('/thanks/')
答案 0 :(得分:1)
return
从函数返回,因此return
必须在函数定义内。您在某个地方的函数定义之外有一个return
。
错误消息将告诉您问题出现在哪一行。
答案 1 :(得分:1)
from django.shortcuts import render
from .forms import form
from django.core.mail import send_mail
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
return render(request, 'index.html', {})
def submit(form):
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['name']
message = form.cleaned_data['name']
sender = form.cleaned_data['sender']
cc_myself = form.cleaned_data['cc_myself']
recipients = ['hillariouskelly@gmail.com']
if cc_myself:
recipients.append(sender)
send_mail(name, email, message, sender, recipients)
return HttpResponseRedirect('/thanks/')