Python 通过电子邮件Django请求新用户帐户
我正在尝试为用户创建一个小功能来请求一个帐户,他输入的信息将发送给一个将创建帐户的人。 我主要是遵循这个例子,但它是非常旧的,可能是不准确的,因为我已经得到了,因此我与它的问题 我目前有以下代码: account/models.py account/views.py url.py 模板/new_account.html 在输入名称和密码并单击提交后发生。 我的电子邮件主机工作正常,因为我以前使用的是“忘记密码” 功能。 我非常感谢你的帮助。谢谢。Python 通过电子邮件Django请求新用户帐户,python,django,forms,email,Python,Django,Forms,Email,我正在尝试为用户创建一个小功能来请求一个帐户,他输入的信息将发送给一个将创建帐户的人。 我主要是遵循这个例子,但它是非常旧的,可能是不准确的,因为我已经得到了,因此我与它的问题 我目前有以下代码: account/models.py account/views.py url.py 模板/new_account.html 在输入名称和密码并单击提交后发生。 我的电子邮件主机工作正常,因为我以前使用的是“忘记密码” 功能。 我非常感谢你的帮助。谢谢。请求上下文(请求)需要更改。在下面的代码中,第二个
请求上下文(请求)
需要更改。在下面的代码中,第二个返回永远不会被命中,因此您不会将RequestContext传递给模板
else:
return render_to_response('new_account.html', {'form': NewAccountForm()})
return render_to_response('new_account.html', {'form': NewAccountForm()},
RequestContext(request))
您是否将
django.middleware.csrf.CsrfViewMiddleware
添加到中间件类、中间件类列表中?(它应该出现在任何认为CSRF攻击已被处理的视图中间件之前。)
您可以在此处了解有关Django的CSRF保护的更多信息:
你能建议一种方法来修复它吗?试着去掉其他方法:返回render\u to\u response('new\u account.html',{'form':NewAccountForm()}),这样第二个方法将返回RequestContext,看看是否有效
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from account.models import NewAccountForm
from django import forms
from django.core.mail import send_mail, BadHeaderError
def request_account_view(request):
first_name = request.POST.get('first_name', '')
last_name = request.POST.get('last_name', '')
email = request.POST.get('email', '')
message = (first_name + ' ' + last_name + ' has requested a new account for the email ' + email)
if first_name and last_name and email:
try:
send_mail('Request for Account', message, email, ['example@example.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('/thankyou/')
else:
return render_to_response('new_account.html', {'form': NewAccountForm()})
return render_to_response('new_account.html', {'form': NewAccountForm()},
RequestContext(request))
def thankyou(request):
return render_to_response('thankyou.html')
(r'^thankyou/$', 'account.views.thankyou'),
(r'^new_account/$', 'account.views.request_account_view'),
{% extends "base.html" %}
{% block content %}
<form action="/new_account/" method="post">
{% csrf_token %}
<label id="id_first_name">First Name:</label>
<input type="text" name="first_name" value="" id="first_name" />
<label id="id_last_name">Last Name:</label>
<input type="text" name="last_name" value="" id="last_name" />
<label id="id_email">Email:</label>
<input type="text" name="email" value="" id="email" />
<input type="submit" value="Submit Request"/>
</form>
{% endblock %}
Forbidden (403)
CSRF verification failed. Request aborted.
else:
return render_to_response('new_account.html', {'form': NewAccountForm()})
return render_to_response('new_account.html', {'form': NewAccountForm()},
RequestContext(request))