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
|
db.sqlite3
|
||||||
*.sw*
|
*.sw*
|
||||||
.env
|
.env
|
||||||
|
*.pyc
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{% load static from staticfiles %}
|
{% load static from staticfiles %}
|
||||||
|
{% load bootstrap3 %}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
@ -49,6 +50,7 @@
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="main" class="container container-fluid">
|
<div id="main" class="container container-fluid">
|
||||||
|
{% bootstrap_messages %}
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
<script src="{% static 'js/bootstrap.min.js' %}">
|
<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">
|
<p class="lead">
|
||||||
Yeah! You now have a BornHack user!
|
Yeah! You now have a BornHack user!
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="{% url 'profiles:update' %}">
|
||||||
|
Edit your profile
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% 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 django.conf.urls import url
|
||||||
|
|
||||||
from .views import ProfileDetail
|
from .views import ProfileDetail, ProfileUpdate
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', ProfileDetail.as_view(), name='detail'),
|
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.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.utils.decorators import method_decorator
|
from django.views.generic import DetailView, UpdateView
|
||||||
from django.views.generic import DetailView
|
from django.core.urlresolvers import reverse_lazy
|
||||||
|
from django.contrib import messages
|
||||||
from .models import Profile
|
|
||||||
|
|
||||||
|
|
||||||
class ProfileDetail(DetailView):
|
from . import models, forms
|
||||||
model = Profile
|
|
||||||
|
|
||||||
@method_decorator(login_required)
|
|
||||||
def get(self, request, *args, **kwargs):
|
class ProfileDetail(LoginRequiredMixin, DetailView):
|
||||||
return super().get(request, *args, **kwargs)
|
model = models.Profile
|
||||||
|
|
||||||
def get_object(self, queryset=None):
|
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