diff --git a/users/forms.py b/users/forms.py index 1b3b845..ffbc0a9 100644 --- a/users/forms.py +++ b/users/forms.py @@ -1,4 +1,5 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm +from django.forms import ModelForm from .models import CustomUser @@ -13,3 +14,9 @@ class CustomUserChangeForm(UserChangeForm): class Meta: model = CustomUser fields = ('username', 'student_number', 'real_name',) + + +class UserMetaForm(ModelForm): + class Meta: + model = CustomUser + fields = ['student_number', 'real_name'] diff --git a/users/templates/profile.html b/users/templates/profile.html index 96a13fc..30766a9 100644 --- a/users/templates/profile.html +++ b/users/templates/profile.html @@ -5,12 +5,13 @@ {% block content %}

Profile

{% if user.is_authenticated %} -

Username: {{ username }}

- - - - - +

Username: {{ user.username }}

+ +
+ {% csrf_token %} + {{ form }} + +
{% else %}

Not logged in

{% endif %} diff --git a/users/views.py b/users/views.py index fc41d10..c6ed891 100644 --- a/users/views.py +++ b/users/views.py @@ -1,10 +1,31 @@ +from pprint import pprint + from django.contrib.auth import logout +from django.http import HttpResponseRedirect from django.shortcuts import render, redirect +from users.forms import UserMetaForm +from users.models import CustomUser + def profile(request): - return render(request, "profile.html", {"username": request.user.username}) + # if this is a POST request we need to process the form data + if request.method == 'POST': + # create a form instance and populate it with data from the request: + form = UserMetaForm(request.POST, instance=request.user) + # check whether it's valid: + if form.is_valid(): + # process the data in form.cleaned_data as required + form.save() + # redirect to a new URL: + return HttpResponseRedirect('/user/profile') + + # if a GET (or any other method) we'll create a blank form + else: + form = UserMetaForm(instance=request.user) + + return render(request, 'profile.html', {'form': form}) def logout_view(request): logout(request)