Difference between revisions of "Create account edit page in Django"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Define URL)
(Declare a view)
Line 27: Line 27:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
from .forms import UserRegistrationForm, UserViewForm
 
from .forms import UserRegistrationForm, UserViewForm
 +
</syntaxhighlight>
 +
 +
Now add this to the imports:
 +
 +
<syntaxhighlight lang=python>
 +
from django.contrib.auth.decorators import login_required
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 52: Line 58:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The 'instance=request.user' will get the data for the current user.
+
The 'instance=request.user' will get the data for the current user. Finally add the text '@login_required' before the def:
 +
 
 +
<syntaxhighlight lang=python>
 +
@login_required
 +
def update(request):
 +
</syntaxhighlight>
 +
 
 +
This will mean that in order to access this page the user will need to be already logged in.
  
 
=Create the template=
 
=Create the template=

Revision as of 21:27, 3 May 2019

In the previous tutorial we created a sign up / register page. We now need to create a page to edit the information for the user.

Create the form

In the 'users' app folder, open 'forms.py'. This should currently have the 'UserRegistrationForm'. Now copy the entire class, and rename it 'UserViewForm'. Also remove the references to 'password1' and 'password2'. Also note the change from 'UserCreationForm' to 'forms.ModelForm':

class UserViewForm (forms.ModelForm):
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)

    class Meta:
        model=User
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            ]

Declare a view

Now in the 'views.py' within the 'users' app folder, add the 'UserViewForm' to the import:

from .forms import UserRegistrationForm, UserViewForm

Now add this to the imports:

from django.contrib.auth.decorators import login_required

Now copy the entire 'register' def, now change the def to 'update'. Change 'UserRegistrationForm' to 'UserViewForm'. Change the messages to reflect an update instead of create. Final change the 'register.html' to 'update.html':

def update(request):
    if request.method=='POST':
        form = UserViewForm(request.POST, instance=request.user)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            email = form.cleaned_data.get('email')
            first_name = form.cleaned_data.get('first_name')
            last_name = form.cleaned_data.get('last_name')
            form.save()
            messages.success(request, 'Your account has been updated!')
            return redirect('login')
        else:
            form = UserViewForm(instance=request.user)
            messages.success(request, 'Your account has not been updated! form invalid!')
            return render(request, 'users/update.html', {'form':form})
    else:
        form = UserViewForm(instance=request.user)
        return render(request, 'users/update.html', {'form':form})

The 'instance=request.user' will get the data for the current user. Finally add the text '@login_required' before the def:

@login_required
def update(request):

This will mean that in order to access this page the user will need to be already logged in.

Create the template

In the 'templates' folder of the 'users' app, copy the 'register.html' and duplicate it and rename it to 'update.html':

Change the references for 'Register' to 'Update'.

Define URL

Now open the 'urls.py' from the project folder (mine is called 'MyProject'). Add this path to the 'urlpatterns':

    path('update', user_views.update, name='update'),