Adding profile update functionality
This commit is contained in:
parent
1de0b1ad0e
commit
9f2ce9d7c5
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ __pycache__/
|
|||
db.sqlite3
|
||||
*.sw*
|
||||
.env
|
||||
*.pyc
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{% load static from staticfiles %}
|
||||
{% load bootstrap3 %}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
@ -49,6 +50,7 @@
|
|||
</nav>
|
||||
|
||||
<div id="main" class="container container-fluid">
|
||||
{% bootstrap_messages %}
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<script src="{% static 'js/bootstrap.min.js' %}">
|
||||
|
|
11
profiles/forms.py
Normal file
11
profiles/forms.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from django import forms
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
class ProfileForm(forms.ModelForm):
|
||||
email = forms.EmailField()
|
||||
|
||||
class Meta:
|
||||
model = models.Profile
|
||||
fields = []
|
|
@ -5,5 +5,10 @@
|
|||
<p class="lead">
|
||||
Yeah! You now have a BornHack user!
|
||||
</p>
|
||||
<p>
|
||||
<a href="{% url 'profiles:update' %}">
|
||||
Edit your profile
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
20
profiles/templates/profiles/profile_form.html
Normal file
20
profiles/templates/profiles/profile_form.html
Normal file
|
@ -0,0 +1,20 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% load account %}
|
||||
{% load bootstrap3 %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<h1>Edit profile</h1>
|
||||
|
||||
<form class="login" method="POST">
|
||||
{% csrf_token %}
|
||||
{% bootstrap_form form %}
|
||||
<button class="form-control btn btn-black" type="submit">Save</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
|
@ -1,7 +1,8 @@
|
|||
from django.conf.urls import url
|
||||
|
||||
from .views import ProfileDetail
|
||||
from .views import ProfileDetail, ProfileUpdate
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', ProfileDetail.as_view(), name='detail'),
|
||||
url(r'^edit$', ProfileUpdate.as_view(), name='update'),
|
||||
]
|
|
@ -1,16 +1,34 @@
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.generic import DetailView
|
||||
|
||||
from .models import Profile
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.views.generic import DetailView, UpdateView
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.contrib import messages
|
||||
|
||||
|
||||
class ProfileDetail(DetailView):
|
||||
model = Profile
|
||||
from . import models, forms
|
||||
|
||||
@method_decorator(login_required)
|
||||
def get(self, request, *args, **kwargs):
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
class ProfileDetail(LoginRequiredMixin, DetailView):
|
||||
model = models.Profile
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
return Profile.objects.get(user=self.request.user)
|
||||
return models.Profile.objects.get(user=self.request.user)
|
||||
|
||||
|
||||
class ProfileUpdate(LoginRequiredMixin, UpdateView):
|
||||
model = models.Profile
|
||||
form_class = forms.ProfileForm
|
||||
success_url = reverse_lazy('profiles:detail')
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
return models.Profile.objects.get(user=self.request.user)
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(ProfileUpdate, self).get_form_kwargs()
|
||||
kwargs['initial'] = {'email': self.object.user.email}
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form, **kwargs):
|
||||
self.object.user.email = form.cleaned_data['email']
|
||||
self.object.user.save()
|
||||
messages.info(self.request, 'Your profile has been updated.')
|
||||
return super(ProfileUpdate, self).form_valid(form, **kwargs)
|
||||
|
|
Loading…
Reference in a new issue