-
-
Save crukundo/aea387ad573a73457c8a2d0c59f53eb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib.auth.models import User | |
class UserProfileForm(forms.ModelForm): | |
class Meta: | |
model = User | |
fields = ['first_name', 'last_name', 'email'] | |
# models.py | |
from django.db import models | |
from django.contrib.auth.models import User | |
from django.utils.translation import ugettext_lazy as _ | |
from mezzanine.core.fields import FileField | |
from django.db.models.signals import post_save | |
class UserProfile(models.Model): | |
user = models.OneToOneField(User, related_name='user') | |
photo = FileField(verbose_name=_("Profile Picture"), | |
upload_to=upload_to("main.UserProfile.photo", "profiles"), | |
format="Image", max_length=255, null=True, blank=True) | |
website = models.URLField(default='', blank=True) | |
bio = models.TextField(default='', blank=True) | |
phone = models.CharField(max_length=20, blank=True, default='') | |
city = models.CharField(max_length=100, default='', blank=True) | |
country = models.CharField(max_length=100, default='', blank=True) | |
organization = models.CharField(max_length=100, default='', blank=True) | |
def create_profile(sender, **kwargs): | |
user = kwargs["instance"] | |
if kwargs["created"]: | |
user_profile = UserProfile(user=user) | |
user_profile.save() | |
post_save.connect(create_profile, sender=User) | |
# views.py | |
from django.shortcuts import render, HttpResponseRedirect, HttpResponse | |
from django.contrib.auth.decorators import login_required | |
from django.contrib.auth.models import User | |
from .models import UserProfile | |
from .forms import UserProfileForm | |
from django.forms.models import inlineformset_factory | |
from django.core.exceptions import PermissionDenied | |
@login_required | |
def edit_user(request, pk): | |
user = User.objects.get(pk=pk) | |
user_form = UserProfileForm(instance=user) | |
ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=('website', 'bio', 'phone', 'city', 'country', 'organization')) | |
formset = ProfileInlineFormset(instance=user) | |
if request.user.is_authenticated() and request.user.id == user.id: | |
if request.method == "POST": | |
user_form = UserProfileForm(request.POST, request.FILES, instance=user) | |
formset = ProfileInlineFormset(request.POST, request.FILES, instance=user) | |
if user_form.is_valid(): | |
created_user = user_form.save(commit=False) | |
formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user) | |
if formset.is_valid(): | |
created_user.save() | |
formset.save() | |
return HttpResponseRedirect('/accounts/profile/') | |
return render(request, "account/account_update.html", { | |
"noodle": pk, | |
"noodle_form": user_form, | |
"formset": formset, | |
}) | |
else: | |
raise PermissionDenied | |
# template.html | |
<div class="col s12 m8 offset-m2"> | |
<div class="card"> | |
<div class="card-content"> | |
<h2 class="flow-text">Update your information</h2> | |
<form action="." method="POST" class="padding"> | |
{% csrf_token %} {{ noodle_form.as_p }} | |
<div class="divider"></div> | |
{{ formset.management_form }} | |
{{ formset.as_p }} | |
<button type="submit" class="btn-floating btn-large waves-light waves-effect"><i class="large material-icons">done</i></button> | |
<a href="#" onclick="window.history.back(); return false;" title="Cancel" class="btn-floating waves-effect waves-light red"><i class="material-icons">history</i></a> | |
</form> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment