From b3872eb0c6963d32b4ac8f7d0092238525865a1b Mon Sep 17 00:00:00 2001 From: chirac Date: Fri, 1 Jul 2016 18:22:52 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9e=20une=20vue=20permetant=20d'editer=20?= =?UTF-8?q?un=20user=20existant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/users/{new_user.html => user.html} | 2 +- users/urls.py | 1 + users/views.py | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) rename users/templates/users/{new_user.html => user.html} (77%) diff --git a/users/templates/users/new_user.html b/users/templates/users/user.html similarity index 77% rename from users/templates/users/new_user.html rename to users/templates/users/user.html index e6047fab..19be1d1a 100644 --- a/users/templates/users/new_user.html +++ b/users/templates/users/user.html @@ -9,6 +9,6 @@
{% csrf_token %} {% bootstrap_form userform %} - {% bootstrap_button "Créer" button_type="submit" icon="star" %} + {% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
{% endblock %} diff --git a/users/urls.py b/users/urls.py index 616af19b..82396778 100644 --- a/users/urls.py +++ b/users/urls.py @@ -4,5 +4,6 @@ from . import views urlpatterns = [ url(r'^new_user/$', views.new_user, name='new_user'), + url(r'^edit_info/(?P[0-9]+)$', views.edit_info, name='edit_info'), ] diff --git a/users/views.py b/users/views.py index 4f9b738f..87242c40 100644 --- a/users/views.py +++ b/users/views.py @@ -6,7 +6,7 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.core.context_processors import csrf from django.template import Context, RequestContext, loader -from users.models import UserForm, InfoForm +from users.models import User, UserForm, InfoForm, PasswordForm, StateForm def form(ctx, template, request): @@ -19,7 +19,17 @@ def new_user(request): user = InfoForm(request.POST) if user.is_valid(): user.save() - return form({'userform': user}, 'users/new_user.html', request) + return form({'userform': user}, 'users/user.html', request) else: user = InfoForm() - return form({'userform': user}, 'users/new_user.html', request) + return form({'userform': user}, 'users/user.html', request) + +def edit_info(request, userid): + try: + user = User.objects.get(pk=userid) + except User.DoesNotExist: + user = None + user = InfoForm(request.POST or None, instance=user) + if user.is_valid(): + user.save() + return form({'userform': user}, 'users/user.html', request)