OK so I have the registration working where users enter their username, email, first/last name.
My question is how do I then give the user the ability to edit their email, first/last names?
Thanks!
OK so I have the registration working where users enter their username, email, first/last name.
My question is how do I then give the user the ability to edit their email, first/last names?
Thanks!
默认 django 用户没有什么特别之处。事实上,django auth 应用程序是一个普通的 django 应用程序,它有自己的模型、视图、url和模板,就像您编写的任何其他应用程序一样。
要更新任何模型实例 - 您可以使用 generic UpdateView
,其工作原理如下:
以下是您如何在实践中实现它。在你的views.py
:
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.models import User
class ProfileUpdate(UpdateView):
model = User
template_name = 'user_update.html'
success_url = reverse_lazy('home') # This is where the user will be
# redirected once the form
# is successfully filled in
def get_object(self, queryset=None):
'''This method will load the object
that will be used to load the form
that will be edited'''
return self.request.user
user_update.html
模板非常简单:
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" />
</form>
现在剩下的就是将它连接到您的urls.py
:
from .views import ProfileUpdate
urlpatterns = patterns('',
# your other url maps
url(r'^profile/', ProfileUpdate.as_view(), name='profile'),
)