因为在部署过程中会涉及用户邮件发送,如果有的同事不愿意收到太多邮件,则可以自己定义为不存在的邮箱。
我们在注册的时候,也不会写用户邮箱地址,那么他们也可以在这里自己更改。
changeemail.html模板
{% extends "index.html" %} {% load staticfiles %} {% block title %} 修改邮箱 {% endblock %} {% block content %} <div class="tm-middle"> <div class="uk-container uk-container-center"> <h3 class="tm-article-subtitle uk-margin-top"> 修改邮箱 </h3> <hr class="uk-grid-divider"> <div class="uk-panel uk-panel-box uk-container-center uk-width-1-3"> <h3 class="uk-panel-title"><i class="uk-icon-user"></i>修改邮箱</h3> <div class="uk-grid"> <div > 当前邮箱:{{ user.email }} {% if changeemail_success %} <div class="uk-alert">邮箱更新成功!</div> {% endif%} <form class="uk-form uk-form-horizontal uk-margin-top" action="" method="post"> {% csrf_token %} <div class="uk-form-row"> <div class="uk-form-label"> 更改{{ form.email.label_tag }} </div> <div class="uk-form-control"> {{ form.email }} </div> </div> <div class="uk-form-row"> <input type="submit" value="确认修改" class="uk-button uk-button-primary"> </div> </form> </div> </div> </div> </div> </div> {% endblock %}
forms.py表单文件:
class UserEmailForm(forms.ModelForm): email = forms.EmailField(required=True) class Meta: model = User fields = ('email',)
views.py文件:
def changeemail(request): if request.POST: user_form = UserEmailForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() return render_to_response('changeemail.html', RequestContext(request, {'form': user_form, 'changeemail_success': True})) else: user_form = UserEmailForm() return render_to_response('changeemail.html', RequestContext(request, {'form': user_form, }))
最后,,就简单的弄出来了。