def profile(request): profile = Customer.objects.filter(user=request.user).first() if request.method == "POST": # The instance has to be of class User, not Customer like the variable profile is. # Hence, instance=request.user vs. instance=profile. profile_form = ProfileForm(instance=request.user, data=request.POST) image_form = ImageForm(instance=profile, data=request.POST) if profile_form.is_valid() and image_form.is_valid(): image = image_form.save(commit=False) profile = profile_form.save(commit=False) profile.user = request.user image.user = request.user profile.save() image.save() return redirect('profile') else: # TODO: Error handling, if either form isn't valid. context = { 'profile_form': ProfileForm(), 'image_form': ImageForm(), 'search_history': SearchHistory.objects.filter(user=request.user.id).order_by('time') } search_history = SearchHistory.objects.select_related('console').filter(user_id=request.user.id).order_by('-time') search_history = list(map(lambda x: x.console, search_history)) context = { 'profile_form': ProfileForm(), 'image_form': ImageForm(), 'search_history': search_history } return render(request, 'user/profile.html', context)
def profile(request): profile = UserProfile.objects.filter(user = request.user).first() if request.method == 'POST': u_form = UserForm(request.POST, instance=request.user) p_form = ProfileForm(request.POST, instance=profile) if p_form.is_valid() and u_form.is_valid(): u_form.save() profile = p_form.save(commit = False) profile.user = request.user profile.save() messages.success(request, f'Breytingar skráðar.') return redirect('profile') else: messages.error(request, f'Eitthvað fór úrskeiðis') return redirect('/') else: u_form = UserForm(instance = request.user) p_form = ProfileForm(instance = profile) return render(request, 'user/profile.html', { 'p_form': p_form, 'u_form': u_form })
def profile(request): profile = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('profile') return render(request, 'user/profile.html', {'form': ProfileForm(instance=profile)})
def edit_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() else: messages.error(request, 'Please correct the error below.') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) print(profile_form) #profile_form.fields['profile_picture']. return render(request, 'user/change_profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def profile(request): profile = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) # Gert til að commita ekki í gagnagrunninn því FK er ekki implementaður en gerir temp profile profile.user = request.user #tengir við innskráðan notanda profile.save() return redirect('profile') return render(request, 'user/profile.html', { 'form': ProfileForm(instance=profile) })
def newUser_upplysingar(request): #eins og profile nema er nottað í lokuðu umhverfi og linkar a önnur url -Andri newUser_upplysingar = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = ProfileForm(instance=newUser_upplysingar, data=request.POST) if form.is_valid(): newUser_upplysingar = form.save(commit=False) newUser_upplysingar.user = request.user newUser_upplysingar.save() return redirect('index-forsida') return render(request, 'user/newUser_upplysingar.html', {'form': ProfileForm(instance=newUser_upplysingar)})
def edit_profile(request): """This function sends the form to edit the profile and saves in when returned""" profile = Profile.objects.filter(user=request.user).first() # A POST request will update the profile of the user if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('profile') # If not a POST request a page will be rendered to update the profile return render(request, 'user/edit_profile.html', {'form': ProfileForm(instance=profile)})
def picture(request): profile = UserImage.objects.filter(user_id=request.user.id).first() if profile == None: profile = UserImage() profile.user = request.user profile.image = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/User_icon_2.svg/1024px-User_icon_2.svg.png' if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('/user/profile') return render(request, 'user/picture.html', {'form': ProfileForm(instance=profile)})
def edit_profile_picture(request): profile = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = ProfileForm(instance=profile, files=request.FILES, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() messages.success(request, f'Your picture was successfully updated!') return redirect('profile') return render(request, 'user/change_profile_picture.html', {'form': ProfileForm(instance=profile)})
def profile(request): # Edit profile profile = Profile.objects.filter(user=request.user).first() if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() user = Profile.objects.get(user=request.user.id) user = user.user user.first_name = request.POST['firstName'] user.last_name = request.POST['lastName'] user.save() return redirect('profile') return render(request, 'user/profile.html', { 'form': ProfileForm(instance=profile), 'profile': profile })
def edit_profile(request): user = request.user # User.objects.filter(id=profile.id).first() profile = Profile.objects.filter(user=user).first() if request.method == 'POST': form1 = ProfileForm(instance=profile, data=request.POST) form2 = UserForm(instance=user, data=request.POST) if form1.is_valid() and form2.is_valid(): profile = form1.save(commit=False) user = form2.save(commit=False) profile.user = request.user profile.save() user.save() return redirect('profile') else: return redirect('editprofile') else: return render( request, 'user/edit_profile.html', { 'form1': ProfileForm(instance=profile), 'form2': UserForm(instance=user) })
def get_seller_by_id(request, id): if request.user.is_authenticated: profile = Profile.objects.filter(user=request.user).first() search_history = SearchTerm.objects.filter(user=request.user) if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('home-index') context = { 'seller': get_object_or_404(User, pk=id), 'form': ProfileForm(instance=profile), 'houses': House.objects.filter(seller=id), 'search_history': search_history } else: context = { 'seller': get_object_or_404(User, pk=id), 'houses': House.objects.filter(seller=id) } return render(request, 'user/user_details.html', context)
def profile(request): profile = Profile.objects.filter(user=request.user).first() if profile is None: profile = Profile(user_id=request.user.id) profile.save() print(profile) if request.method == 'POST': form = ProfileForm(instance=profile, data=request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('profile') today = date.today() yesterday = today - timedelta(1) lastweek = today - timedelta(7) context = { 'form': ProfileForm(instance=profile), 'today': SearchHistory.objects.filter(profile=request.user.profile, timestamp=today).order_by('-timestamp'), 'yesterday': SearchHistory.objects.filter( profile=request.user.profile, timestamp=yesterday).order_by('-timestamp'), 'lastweek': SearchHistory.objects.filter( profile=request.user.profile, timestamp__lt=yesterday, timestamp__gte=lastweek).order_by('-timestamp'), 'older': SearchHistory.objects.filter( profile=request.user.profile, timestamp__lt=lastweek).order_by('-timestamp'), } return render(request, 'user/profile.html', context)