Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # webapp/models
- from django.db import models
- from django.contrib.auth import get_user_model
- User = get_user_model()
- class UserProfile(models.Model):
- """ User profile model """
- user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
- receive_notifications = models.BooleanField(default=False)
- def __str__(self):
- """A string representation of the model."""
- return f"{self.user.email} Profile"
- # webapp/forms
- from django import forms
- from .models import UserProfile
- class UserProfile(forms.ModelForm):
- class Meta:
- model = UserProfile
- fields = ['receive_notifications']
- # webapp/views
- from django.shortcuts import render, redirect
- from django.contrib.auth.models import User
- from django.contrib import messages
- from .forms import UserProfile
- def profile(request):
- if request.method == 'POST':
- profile_form = UserProfile(request.POST, instance=request.user)
- if profile_form.is_valid():
- data = profile_form.save()
- messages.success(request, f"Your account has been updated")
- return redirect('/')
- else:
- profile_form = UserProfile(instance=request.user)
- return render(request, 'webapp/index.html', context={'profile_form': profile_form})
Advertisement
Add Comment
Please, Sign In to add comment