开发者

Request for new user account via email Django

I am trying to make a little function for a user to request an account and the information he puts in is sent to a person who will make an account. I was following this example mostly http://djangosnippets.org/snippets/261/ but it is very old and might be inaccurate for I have got and thus I have problems with it.

I have this code currently:

account/models.py

from django import forms
from django.core.mail import send_mail, BadHeaderError

# A simple contact form with four fields.
class NewAccountForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    email = forms.EmailField()

account/views.py

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')

urls.py

(r'^thankyou/$', 'account.views.thankyou'),
(r'^new_account/$', 'account.views.request_account_view'),

templates/new_account.html

{% 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 %}

I am getting an error for:

Forbidden (403)
CSRF verification failed. Request 开发者_如何学JAVAaborted.

which occurs after I input name and password and click submit. My email host is working fine because I previously was working on a "Forgot Password" function. I would really appreciate help. Thank you.


RequestContext(request) needs to be used. In your code below the second return will never be hit so you are not passing RequestContext to the template.

    else:
        return render_to_response('new_account.html', {'form': NewAccountForm()})

    return render_to_response('new_account.html', {'form': NewAccountForm()},
        RequestContext(request))


Did you add django.middleware.csrf.CsrfViewMiddleware to your list of middleware classes, MIDDLEWARE_CLASSES? (It should come before any view middleware that assume that CSRF attacks have been dealt with.)

You can learn more about Django's CSRF protection here: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜